vb.net 从十进制中删除尾随零的最佳方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27807469/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Best way to remove trailing zeros from a Decimal
提问by Tony D
I have a calculation that returns a decimal with a maximum of two decimal places. What I'd like to achieve is a result that doesn't include decimal zeros, so 8.00 is shown as 8, 8.10 is shown as 8.1 and 8.12 is shown as 8.12.
我有一个计算返回最多两位小数的小数。我想要实现的是不包含十进制零的结果,因此 8.00 显示为 8,8.10 显示为 8.1,8.12 显示为 8.12。
I've been all over the math functions and can't find anything to achieve this - can anyone point me in the right direction?
我一直在数学函数中找不到任何东西来实现这一点 - 任何人都可以指出我正确的方向吗?
采纳答案by b.pell
As Benjamin Leinweber pointed out in one of the comments it's probably a product of it being displayed as a string. That said, you could take the duct tape approach and just lop off the trailing digits you don't want like this (who doesn't love duct tape every so often):
正如 Benjamin Leinweber 在其中一条评论中指出的那样,它可能是将其显示为字符串的产物。也就是说,你可以采用胶带的方法,然后像这样去掉你不想要的尾随数字(谁不经常喜欢胶带):
' Hacky, I did this because Visual Studio removes the 0 in the editor
Dim num As Decimal = CDec("8.10")
' This will now output 8.10
Console.WriteLine(num)
' Put it in a string then trim off the 0's and then the decimal place if it then happens to be at the end
Dim buf As String = num.ToString
buf = buf.TrimEnd("0")
buf = buf.TrimEnd(".")
' This will output 8.1
Console.WriteLine(buf)
回答by fnostro
Are you talking about the VB Decimal data type? If so then read this from the MSDN documentation...
您是在谈论 VB Decimal 数据类型吗?如果是这样,请从 MSDN 文档中阅读此内容...
Trailing Zeros. Visual Basic does not store trailing zeros in a Decimal literal.
However, a Decimal variable preserves any trailing zeros acquired computationally.
The following example illustrates this.
Dim d1, d2, d3, d4 As Decimal
d1 = 2.375D
d2 = 1.625D
d3 = d1 + d2
d4 = 4.000D
MsgBox("d1 = " & CStr(d1) & ", d2 = " & CStr(d2) &
", d3 = " & CStr(d3) & ", d4 = " & CStr(d4))
The output of MsgBox in the preceding example is as follows:
d1 = 2.375, d2 = 1.625, d3 = 4.000, d4 = 4
So since the Decimal data type holds on to significant digits in a calculation then you may want to convert the data type to double and use a custom format like {0:#.##}for display
因此,由于 Decimal 数据类型在计算中保留有效数字,因此您可能希望将数据类型转换为 double 并使用自定义格式,例如{0:#.##}用于显示
回答by MetalPhoenix
.ToString("###")or however many octothorpes for however many to the left of the decimal.
.ToString("###")或者不管小数点左边有多少八索。
If you want to have... say, 2 decimal places:
如果你想...说,小数点后2位:
.ToString("###.##")
.ToString("###.##")
回答by Jafar Elahi
You can do it by using TrimEnd function :
您可以使用 TrimEnd 函数来实现:
Dim number1 As Double = 8.0
Dim number2 As Double = 8.1
Dim number3 As Double = 8.12
Console.WriteLine(number1.ToString("N2").TrimEnd("0"c).TrimEnd("."c))
Console.WriteLine(number2.ToString("N2").TrimEnd("0"c).TrimEnd("."c))
Console.WriteLine(number3.ToString("N2").TrimEnd("0"c).TrimEnd("."c))
Consider that formatting with "F0" will remove 1000 separators too.
考虑使用“F0”格式化也会删除 1000 个分隔符。
回答by David Brossard
From what you are describing, it sounds like you are using a Fixed-pointformat when you should be using a Numberformat or a Generalformat.
根据您的描述,听起来您Fixed-point在应该使用一种Number格式或一种General格式时却在使用一种格式。
This sample code taken from the MSDNshows how to use the Numberformat:
取自MSDN 的示例代码展示了如何使用该Number格式:
Dim dblValue As Double = -12445.6789
Console.WriteLine(dblValue.ToString("N", CultureInfo.InvariantCulture))
' Displays -12,445.68
Console.WriteLine(dblValue.ToString("N1", _
CultureInfo.CreateSpecificCulture("sv-SE")))
' Displays -12 445,7
Dim intValue As Integer = 123456789
Console.WriteLine(intValue.ToString("N1", CultureInfo.InvariantCulture))
' Displays 123,456,789.0
For a complete list of formatters, have a look at this MSDN article.
有关格式化程序的完整列表,请查看此MSDN 文章。

