且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将字符串转换为双精度型,小数点后2位

更新时间:2023-11-28 23:47:34

双打不能完全重新present 16.9。我建议你​​将其转换为十进制来代替:

Doubles can't exactly represent 16.9. I suggest you convert it to decimal instead:

string s = "16.9";
decimal m = Decimal.Parse(s) * 100;

double d = (double)m;

您可能只是想继续使用十进制而不是,既然你说你会使用它的金融需求。请记住,十进制是为了准确地再适合在precision present十进制数,而只能完全重新的做present二进制数。

You might just want to keep using the decimal instead of the double, since you say you'll be using it for monetary purposes. Remember that decimal is intended to exactly represent decimal numbers that fit in its precision, while double will only exactly represent binary numbers that do.