6

When using double.Parse, it seems to like to string away any trailing (insignificant) zeros from the string that I'm converting. I would like double.Parse to keep to places after the decimal. For example, here is some code:

tobereturned.MouseSensitivty = double.Parse(String.Format("{0:#.##}", tempstring[1]));
Debug.WriteLine("Converted " + String.Format("{0:#.##}", tempstring[1]) + " to " + tobereturned.MouseSensitivty);

The Debugger then writes

Converted 4.00 to 4

So it seems like double.Parse is doing something fishy here. P.S. MouseSensitivity is also of the type double, so I can't do any string operations on it.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
gcode
  • 2,954
  • 4
  • 21
  • 32
  • I don't thing that double.Parse is doing anything odd/fishy. The Number.FormatDouble() function is probably what is rendering your number without the decimal places. (the function that Double.ToString() calls.) – jwwishart Dec 11 '10 at 07:08

4 Answers4

4

Your question is meaningless. Doubles don't have "places after the decimal" in the first place. They don't store anything that looks remotely like a "decimal representation of a number" internally. In fact, they don't store anything internally that even looks like recognizable text.

It reports 4 because 4.00 is exactly equal to 4. It is displaying the number "exactly four with no fractional part" as text according to its default rules for converting numbers to text.

Please read this. Yes, it is long, and difficult, but it is simply not possible to use floating-point numeric types properly without a real understanding of this material - and it doesn't matter what language you're using, either.

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
  • Thank you for explaining that to me. I can understand why it wants to cut off any insignificant figures, although I'm having trouble understanding what you mean when you say "Doubles don't have 'places after the decimal' in the first place". According to http://msdn.microsoft.com/en-us/library/system.double%28v=VS.90%29.aspx , it seems they do :\ – gcode Dec 11 '10 at 15:56
  • 1
    To have "places after the decimal", the number would have to be represented in decimal (i.e. base 10). It is not. It is represented in binary (i.e. base 2). – Karl Knechtel Dec 11 '10 at 16:08
  • 1
    If that was the case, then wouldn't we never be able to have a larger number than a base 2 number? I mean, yes, I know it has to be stored in the memory of the computer at some point (in binary form), but, doesn't the compiler take measures to ensure that the number is the same once it is read out of the memory again? – gcode Dec 11 '10 at 16:12
  • 2
    "Larger number than a base 2 number" is meaningless. The base in which you represent a number does not, itself, limit the range of possible numbers; it also depends how many digits/bits you use. The compiler doesn't need to take any such measures. The number is the number - which **is not the same thing as** its representation in text. When the number is read in, there is a conversion from text to the internal numeric format; and when the number is displayed, there is a conversion back to text. These conversions do not cancel each other out and there is no reason in general to expect them to. – Karl Knechtel Dec 11 '10 at 16:18
2

The double data type is simply a number; it doesn't keep track of the string that was parsed to create the value. Its string representation only comes into play when .ToString() is called.

Phil Hunt
  • 8,404
  • 1
  • 30
  • 25
0

If you know you always want two places after the decimal you can right-fill with zeros.

it is not the job of the double type to keep track of your desired display format.

Beth Lang
  • 1,889
  • 17
  • 38
0

Double does not store redundant zeros. In your view or presentation layer, you might want to format it to show you want it to appear, e.g., String.Format("{0:#.##}", doubleVariable)

Phil C
  • 3,687
  • 4
  • 29
  • 51
  • So if my view is a PropertyGrid, which reads directly from the variable, should I just change that variable into a string, and run a Regex match on it when it's changed, to make sure it only contains numbers and a decimal? – gcode Dec 11 '10 at 15:58
  • nopers, just turn on html switch and use formating {0:F2} in the mark up – Phil C Dec 12 '10 at 12:23