1

I am trying to get the string output of a Double value with group separators and no loss in precision in any culture. Is there anyway to do this without writing my own method to do it?

I know you can use ToString("N0"), but this results in a loss of precision. I know you can use ToString("R") to keep the precision, but this doesn't include group separators.

E.g. For Double.MaxValue in Invariant Culture I would like

"179,769,313,486,231,570,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000"

Edit: When I say loss in precision I mean the full number of significant figures rather than not loosing precision in the 'decmial' part of the value. I want to be able to do 'round trips' with group separators, so ToString -> Parse -> ToString.

Edit 2: For a better comparison of the problem I'm trying to solve here is the output when using ToString("N0"). Here you can see the rounding at the end of the significant figures:

"179,769,313,486,232,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000"
leppie
  • 115,091
  • 17
  • 196
  • 297
MarkOwen320
  • 121
  • 1
  • 7

2 Answers2

1

Doesn't look like any of the built in ToString formats do what I required.

For my purposes I was able to write a method to deal with the scenarios I had, but since it doesn't work for all possible values I'm not posting it here.

Thanks to all the posters for their assistance.

MarkOwen320
  • 121
  • 1
  • 7
0

If you specify the format #,0.# you will get:

  • Thousand separators
  • As many decimals as required

That should fulfill your requirement, if I'm reading the question correctly.

Bas
  • 26,772
  • 8
  • 53
  • 86
  • I'm afraid that doesn't work as you loose the significant figures. In the example below you will notice rounding at the '232,000' part, rather than the '231,570' in my original question: Double.MaxValue.ToString("#,0.#") = "179,769,313,486,232,000,000..." – MarkOwen320 Nov 20 '14 at 20:28