When converting string to floating, the converter creates wrong results.
?Global.System.Convert.ToDouble("635705821821928755").ToString("0")
"635705821821929000"
?Global.System.Convert.ToSingle("635705821821928755").ToString("0")
"635705800000000000"
I am working with VB.Net Visual Studio 2012, Framework 4 on ASP.Net Webpage.
Is there any solution for converting huge numbers from string into floating?
The result isn't wrong, it only has lower precision than you expected. Floating point numbers have a limited precision by design, and you simply can't expect to get a result that is more precise than its limit.
You can use a Decimal
to get higher precision. In this case it is enough for the number that you use:
?Convert.ToDecimal("635705821821928755").ToString("0")
"635705821821928755"
A double
has a precision of 15-16 digits, and a Decimal
has a precision of 28-29 digits.
Note: A Decimal
is strictly speaking not a floating point number, but a fixed precision number. It's an integer with a decimal scale, so for example the number 12.345 is stored as 12345 * 10-3
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments