Double string.format
Asked Answered
R

5

21

I have some double values I want to convert to a string with this pattern:

0.xx or x.xx

Currently I have try this:

double.ToString("#.#0");

What should I add in order to see zero in case my number start with zero?

Relate answered 26/3, 2012 at 9:7 Comment(1)
Use yourdoublevariable.ToString("N2")Paramour
F
34

just use

myDouble.ToString("0.00")

that should do the trick

Favata answered 26/3, 2012 at 9:10 Comment(0)
B
8
myDouble.ToString("N2")

should also work.

Have a look at

MSDN: Custom Numeric Format Strings

MSDN: Standard Numeric Format Strings

Bonacci answered 26/3, 2012 at 9:16 Comment(0)
R
5
String.Format("{0:0.00}", 111.0);
Redtop answered 26/3, 2012 at 9:9 Comment(0)
I
3

Put a zero in front of the decimal separator:

double.ToString("0.#0");

This will always include the digit, in contrast to # which makes it optional.

Indene answered 26/3, 2012 at 9:9 Comment(0)
E
2

use following :

myDouble.ToString("N2")
Eigenvalue answered 26/3, 2012 at 9:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.