0

I need to return the string representation of a double in decimal notation rather than scientific and I used a modified version of the accepted solution here:

Double ToString - No Scientific Notation

private static readonly string format = "." + new string('#', 324);
foo.ToString(format);

This works well other than for a value that equals zero i.e. 0, 0.0, 0.000 etc where it returns an empty string.

What would be the easiest way to return a string representing 0 for any value of zero.

I don't mind if the string is 0 or 0.0 etc as long as it is numerical and not scientific.

Edit: I know I could add a conditional statement to check if the string is empty but I am interested to see if there is a way to do it using string formatting.

Community
  • 1
  • 1
DanBrum
  • 419
  • 1
  • 7
  • 18

4 Answers4

1

You could always add a statement like:

foo.ToString(format)

if (string.IsNullOrEmpty(foo))
foo = "0";
gleng
  • 6,185
  • 4
  • 21
  • 35
  • I know that and it's a good answer so you get an uptvote but I was hoping there is a less hacky way to do it. – DanBrum Oct 11 '13 at 13:10
1

try this:

  string format = "0." + new string('#', 324);

It will add zero before dot (if needed)

See example ideone

Kamil Budziewski
  • 22,699
  • 14
  • 85
  • 105
  • Thanks but I tried that also. For the first one I would have liked it to return "0.9". The second one is basically exactly what I did as the double value was 0.0 and it returned an empty string. SO I removed the # in "#." as it made no difference. Probably should have put that in the original question though. – DanBrum Oct 11 '13 at 13:36
0

Maybe it's what you are looking for:

        double aa = 12.33333;

        string foo = String.Format("{0:0.#}", System.Convert.ToSingle(aa));
  • I haven't but as that is just a wrapper for yourDouble.ToString() I imagine it would return the same as just calling ToString() on the double? So it would return a 0 for ToString but then I cannot format it to not use scientific notation for large decimals. – DanBrum Oct 11 '13 at 13:20
  • Why convert to `Single` (i.e. `float`)? – Jeppe Stig Nielsen Oct 11 '13 at 14:54
0

Argh, those hacks people use. To give one hack more here:

foo.ToString(format + ";" + "-" + format + ";" + "0");

or with

static readonly string newFormat = format + ";" + "-" + format + ";" + "0";

defined under format in the code source, just:

foo.ToString(newFormat);

Of course that is a constant, so it is really:

foo.ToString(.####################################################################################################################################################################################################################################################################################################################################;-.####################################################################################################################################################################################################################################################################################################################################;0);

Hope you like it (I don't).

Jeppe Stig Nielsen
  • 60,409
  • 11
  • 110
  • 181