将double转换为N位小数的字符串,以小数分隔符为单位,并且没有千位分隔符 [英] Converting double to string with N decimals, dot as decimal separator, and no thousand separator
问题描述
我需要将小数转换成一个N位小数(二或四)和一千个分隔符的字符串:
I need to convert a decimal to a string with N decimals (two or four) and NO thousand separator:
'XXXXXXX(点)DDDDD'
'XXXXXXX (dot) DDDDD'
CultureInfo.InvariantCulture
的问题是这个地方,'分隔成千上万。
The problem with CultureInfo.InvariantCulture
is that is places ',' to separate thousands.
更新
这应该适用于十进制和双重类型。
This should work for decimal and double types.
我以前的问题: 需要将double或decimal转换成字符串
My previous question: Need to convert double or decimal to string
推荐答案
对于 decimal
,使用ToString方法,并指定不变文化以小数点分隔符获取期间:
For a decimal
, use the ToString method, and specify the Invariant culture to get a period as decimal separator:
value.ToString("0.00", System.Globalization.CultureInfo.InvariantCulture)
long
type是一个整数,所以没有分数部分。您可以将其格式化为字符串,然后添加一些零:
The long
type is an integer, so there is no fraction part. You can just format it into a string and add some zeros afterwards:
value.ToString() + ".00"
这篇关于将double转换为N位小数的字符串,以小数分隔符为单位,并且没有千位分隔符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!