简体   繁体   English

需要将双精度或十进制转换为字符串

[英]Need to convert double or decimal to string

我需要将 double 转换为字符串,其中两个十进制数字用“点”分隔我担心点必须始终用作分隔符。

最简单的方法是指定CultureInfo.InvariantCulture作为文化,例如

string text = d.ToString("N2", CultureInfo.InvariantCulture);

Perhaps to avoid messing up with CultureInfo settings on clients systems, we better set a concrete way to force the machine to use dot as decimal separator and not thousand separator ==> regardless of culture!也许是为了避免在客户端系统上搞乱 CultureInfo 设置,我们最好设置一个具体的方法来强制机器使用点作为十进制分隔符而不是千位分隔符 ==> 不管文化如何! So,所以,

NumberFormatInfo fi= new NumberFormatInfo();
fi.NumberDecimalSeparator = ".";
string doubleDotDecimalNr = doubleNr.ToString(fi);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM