Click here to Skip to main content
Rate this: bad
good
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);
++tailNumber;
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
good
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: http://en.wikipedia.org/wiki/IEEE_floating_point[^].
 
It looks like you basically know how to control presentation of numbers in string form, but just in case:
http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx[^],
http://msdn.microsoft.com/en-us/library/0c899ak8.aspx[^].
 
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: http://en.wikipedia.org/wiki/Real_number[^].
 
[EDIT]
 
Look at the precision of double: http://en.wikipedia.org/wiki/Double-precision_floating-point_format[^].
 
It's approximately 16 decimal digits. What do you expect then?
 
—SA
  Permalink  
v2
Comments
Member 9743899 at 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);
++tailNumber;
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?
Sergey Alexandrovich Kryukov at 2-Apr-13 8:38am
   
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.
 
—SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

If you read http://floating-point-gui.de/[^] the answer to your question should become clear.
  Permalink  
Comments
Sergey Alexandrovich Kryukov at 1-Apr-13 23:53pm
   
It looks like not a bad article, my 5.
But please see what did I right just out of my head. :-)
—SA
Rate this: bad
good
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.
  Permalink  
v2

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

  Print Answers RSS
0 Maciej Los 315
1 OriginalGriff 233
2 Aajmot Sk 224
3 Richard MacCutchan 220
4 Marcin Kozub 210
0 OriginalGriff 7,853
1 Sergey Alexandrovich Kryukov 7,107
2 DamithSL 5,604
3 Manas Bhardwaj 4,986
4 Maciej Los 4,790


Advertise | Privacy | Mobile
Web02 | 2.8.1411023.1 | Last Updated 2 Apr 2013
Copyright © CodeProject, 1999-2014
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