Convert Double from String

Nasenbaer

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?

Guffa

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Convert string to double from combobox

From Dev

Convert from string to double removes trailing zeros

From Dev

Convert from string to double - "NAs introduced by coercion"

From Dev

Convert from string to double removes trailing zeros

From Dev

Convert from textbox string to double in C#

From Dev

Convert decimal numbers from string to double

From Dev

Convert String from Shared Preferences to Double Value

From Dev

Could not convert double to string

From Dev

Cannot convert Double to String

From Dev

Convert string to double (NOT decimal)

From Dev

Oracle: Convert string to double

From Dev

Convert double to String in Java

From Dev

convert String to double with '%'

From Dev

Convert a string with commas to double

From Dev

convert string to double not working?

From Dev

Convert Double to String Android

From Dev

Convert string to double (NOT decimal)

From Dev

Convert a string to a double for a if clause

From Dev

convert String to double with '%'

From Dev

jsoncpp: convert string to double

From Dev

How can I convert from qi::double_ to string?

From Dev

Remove comma from parsed string before convert it to double

From Dev

Convert from double to string without rounding in C#

From Dev

C#: Convert string from array to double wih dot separator

From Dev

convert from double to strings

From Dev

How to Convert String Values From Map<String, List<String>> to type Double? Flutter

From Dev

XJC restriction convert Double to String

From Dev

Php: How to convert string into double?

From Java

Swift - How to convert String to Double