简体   繁体   English

字符串到浮点vs双重转换

[英]String to float vs double conversion

Why is fvalue1 123456792 after the following line of code has executed? 在执行以下代码行后,为什么fvalue1 123456792是?

float fvalue1 = Convert.ToSingle("123456789", CultureInfo.InvariantCulture);

Converting to double works as expected: 转换为预期的两倍效果:

double dvalue1 = Convert.ToDouble("123456789", CultureInfo.InvariantCulture);

You've just discovered for yourself the reason double type exists - its precision is better then float's. 您刚刚发现了double类型存在的原因-其精度比float类型更好。

Check out following on the differences between these types: Difference between decimal, float and double in .NET? 检查以下这些类型之间的区别: .NET中的十进制,浮点和双精度之间的区别

float没有代表123456789的精度,而double则具有。

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

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