[英]double.parse convert two zero decimal to one decimal
I have a string 10.00 and I want to convert it to double 10.00. 我有一个字符串10.00,我想将其转换为双精度10.00。
I use : 我用 :
string str = "10.00";
double db = double.Parse(str);
the result I get is 10.0 and not 10.00. 我得到的结果是10.0,而不是10.00。
The Parse
and TryParse
on the numeric respect local culture settings; 数字上的
Parse
和TryParse
尊重本地区域性设置; you can change this by specifying a CultureInfo
object. 您可以通过指定
CultureInfo
对象来更改此设置。 For instance, parsing 2.999
into a double gives 2999
in Germany: 例如,将
2.999
解析为一个double会在德国提供2999
:
Console.WriteLine (double.Parse ("2.999")); // 2999 (In Germany)
This is because in Germany, the period indicates a thousands separator rather than a decimal point. 这是因为在德国,句点表示千位分隔符,而不是小数点。 Specifying invariant culture fixes this:
指定不变的区域性可以解决此问题:
double x = double.Parse ("2.999", CultureInfo.InvariantCulture);
The same when calling ToString()
: 调用
ToString()
时相同:
string x = 2.9999.ToString (CultureInfo.InvariantCulture);
A double isn't a string. 双精度不是字符串。 If you want to display the double as a string, you can format it to have two decimal points.
如果要将双精度形式显示为字符串,则可以将其格式化为两个小数点。
For example: 例如:
string str = "10.00";
double db = double.Parse(str);
String.Format("{0:0.00}", db); // will show 10.00
Question isn't really clear, but if you are referring to changing the double back to string with 2 decimal place precision, you can use: 问题还不是很清楚,但是如果您要指的是将双精度转换为精度为两位小数的字符串,则可以使用:
string str = "10.00"
double db = double.parse(str);
string convertedBack = db.ToString("0.00");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.