Click here to Skip to main content
11,922,534 members (32,077 online)
Rate this:
Please Sign up or sign in to vote.
See more: C# Parsing
Basically, I have this line of code:
string name = "name";
string tailCtr = "99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999998";
double tailNumber = double.Parse(tailCtr);
tailCtr = tailNumber.ToString("F0");
name = name + tailCtr;

The problem is when double.Parse(tailCtr) is executed, it is converted to double but in exponential form.
Now, when tailNumber is incremented, and tailNumber is converted to string, tailCtr doesn't get the exact number.
It is somewhat rounded off.

How can I retain the number's precision when parsing to double and back again to string?
Posted 1-Apr-13 18:32pm
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

The whole idea is wrong: the type double as any other numeric type, do not have "exponential form". The "exponential form" is only applicable to a string representing numbers, not the numbers themselves.

If you want to know how floating-point numbers are represented, please see: learn the standard IEEE 754:[^].

It looks like you basically know how to control presentation of numbers in string form, but just in case:[^],[^].

If you think that "when tailNumber is incremented, and tailNumber is converted to string", think again: it is not converted. One of the worst related trends in the beginners these days is trying to work with strings representing data instead of data. Try not to fall there; this is a very bad thing. You need to convert data to string only when you have to represent it on screen or elsewhere and do all calculation only with numeric types, without a single conversion to a string, and without rounding off.

Finally, as to "exact number", you need to understand that floating-point numbers are not real numbers, they only models real numbers to certain limited accuracy. It is apparent that a single real number generally contains infinite amount of information; to understand it, you need to learn the theory of real number in mathematics. To get an idea, please see:[^].


Look at the precision of double:[^].

It's approximately 16 decimal digits. What do you expect then?

Member 9743899 2-Apr-13 5:33am
Let me revise my post:

Basically, I have this line of code:
string name = "name";
string tail = "99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999998";
//tail in this discussion is just for the sake of demonstration
//tail is basically this sample: "folderName(1234)". "1234" is the tail
double tailNumber = double.Parse(tail);
tail = tailNumber.ToString("F0");
name = name + tail;

The problem is when double.Parse(tail) is executed, tailNumber got this: "1.0E+98"
So when tailNumber.ToString("F0"); is executed, tail got this: 100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

How can I retain the number's precision when parsing to double and back again to string?
Look, a string object does not have precision, a double object does not have "exponent form".
double tailNumber cannot be "1.0E+98", it is 1.0E+98 :-)
and it cannot be 100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 exactly.
Look at the precision (my updated answer, after [EDIT]).

And you cannot retain precision if you convert to string and back. You need to work with double numbers, not with strings. It makes no sense.

You need to understand the very essence of floating-point calculation: precision.

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

If you read[^] the answer to your question should become clear.
It looks like not a bad article, my 5.
But please see what did I right just out of my head. :-)
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

Both the answers will give you enough idea - I am adding some basics hoping it will simply this a bit. The number you gave here is somewhere 98 decimal-digits long. system stores everything in binary format. so to store this you would ideally need around 250 bits or 32 bytes.

A double however takes only 8 bytes.
So, how it is possible to store a 32 bytes of information in 8 Bytes.
It happens with a defined formatting for floating point number as you can see in the links provided by SA.

In 64 bits (or 8 bytes)
1 bit is for sign (+ or -)
11 bits for exponent : can have a number up to - 2048
52 bits for significand precision : can precisely keep numbers up to 52 binary digits or 21 decimal digits roughly.

So when you go more than 21 decimal digit - you can't expect that to be precise.
their representation will be like following

123456789012345678901 * Pow(2, X) -- X is the exponent

this way it can represent a very large number - but precision will be in terms of Pow(2, X)

Till 21 decimal digit the value of X would be ZERO. But as you go beyond that will have some positive value and the Pow(2, X) will be unit of change. A operation less than that like +1 or -1 can not be done precisely.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 453
1 Dave Kreskowiak 335
2 Sergey Alexandrovich Kryukov 263
3 Richard MacCutchan 225
4 ppolymorphe 175
0 OriginalGriff 7,231
1 KrunalRohit 4,530
2 Sergey Alexandrovich Kryukov 3,356
3 George Jonsson 2,865
4 Suvendu Shekhar Giri 2,216

Advertise | Privacy | Mobile
Web02 | 2.8.151120.1 | Last Updated 2 Apr 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100