vb.net 使用所需的十进制分隔符将 Double 转换为 String
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20918806/
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
Converting Double to String with desired decimal separator
提问by Wine Too
I have to convert number (double) to string like this:
我必须像这样将数字(双)转换为字符串:
Dim myDouble = 3.14
Dim myDoubleStr = myDouble.ToString ''OR myDouble.ToString("N")
According to my 'culture' settings result is "3,14" what is in most cases OK. But here are cases that I need string representation of a number with decimal point instead of comma. In that case I replace char "," with "." like string manipulation.
根据我的“文化”设置结果是“3,14”,在大多数情况下是可以的。但在某些情况下,我需要用小数点而不是逗号来表示数字的字符串。在这种情况下,我将字符“,”替换为“。” 像字符串操作。
Is here a way that "ToString" convert a number with decimal point directly when this is needed?
这是一种“ToString”在需要时直接转换带小数点的数字的方法吗?
采纳答案by Phil
Try
尝试
.ToString("F", CultureInfo.InvariantCulture)
More info here
更多信息在这里
http://msdn.microsoft.com/en-us/library/dwhawy9k(v=vs.110).aspx#DFormatString
http://msdn.microsoft.com/en-us/library/dwhawy9k(v=vs.110).aspx#DFormatString
回答by Ahmad
You can also have as much precession as you want by specifying the format like this:
您还可以通过指定如下格式来获得尽可能多的进动:
Dim myDouble As Double = 3.14159268
Dim myDoubleStr = myDouble.ToString("0.00000") 'The value will be 3.14159
In case you wanted to use Thousands separator, use this format:
如果您想使用千位分隔符,请使用以下格式:
Dim myDouble = 961327.1234567890
Dim MyDoubleStr = myDouble.ToString("#,##0.00000")
'The value of MyDoubleStr will be 961,327.12345

