且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何将(即:十进制(3.31))转换为十进制(即:3.00)

更新时间:2023-02-11 08:49:01

floor



使用它之后,你只需格式化你的号码就可以了再次.00小数,这就是它
What is wrong with floor?

After using it, you just have to format you number to have .00 decimals again and that's it


让yoi试过 Decimal.Truncate(Decimal)方法(系统)| Microsoft Docs [ ^ ] - 这就是它的目的......

Have yoi tried Decimal.Truncate(Decimal) Method (System) | Microsoft Docs[^] - that is what it is designed to do...
decimal d = 3.31M;
Console.WriteLine("{0}, {1}, {1:0.00}", d, decimal.Truncate(d));







Dim d As Decimal = 3.31D
Console.WriteLine("{0}, {1}, {1:0.00}", d, Decimal.Truncate(d))


首先,我强烈建议阅读:

十进制数据类型(Visual Basic)| Microsoft Docs [ ^ ]

整数数据类型(Visual Basic)| Microsoft Docs [ ^ ]

隐式和显式转换(Visual Basic)| Microsoft Docs [ ^ ]

如何:在Visual Basic中将对象转换为另一种类型Microsoft Docs [ ^ ]

类型转换函数(Visual Basic)| Microsoft Docs [ ^ ]

标准数字格式字符串| Microsoft Docs [ ^ ]

自定义数字格式字符串| Microsoft Docs [ ^ ]

最重要的是:

每个计算机科学家应该知道的关于浮点算术的内容 [ ^ ]





现在,看看下面的代码:

First of all, i'd strongly recommend to read about:
Decimal Data Type (Visual Basic) | Microsoft Docs[^]
Integer Data Type (Visual Basic) | Microsoft Docs[^]
Implicit and Explicit Conversions (Visual Basic) | Microsoft Docs[^]
How to: Convert an Object to Another Type in Visual Basic | Microsoft Docs[^]
Type Conversion Functions (Visual Basic) | Microsoft Docs[^]
Standard Numeric Format Strings | Microsoft Docs[^]
Custom Numeric Format Strings | Microsoft Docs[^]
And the most important:
What Every Computer Scientist Should Know About Floating-Point Arithmetic[^]


Now, take a look at below code:
Dim df As Decimal = 3.31D
Dim di As Integer = df

Console.WriteLine("{0} => {1}", df, di)
df=di
Console.WriteLine("{0:0.00} => {1}", df, di)