falukky
falukky

Reputation: 1139

Double string.format

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?

Upvotes: 21

Views: 49665

Answers (5)

raznagul
raznagul

Reputation: 375

myDouble.ToString("N2")

should also work.

Have a look at

MSDN: Custom Numeric Format Strings

MSDN: Standard Numeric Format Strings

Upvotes: 8

Zaki
Zaki

Reputation: 5600

use following :

myDouble.ToString("N2")

Upvotes: 2

Random Dev
Random Dev

Reputation: 52280

just use

myDouble.ToString("0.00")

that should do the trick

Upvotes: 34

ionden
ionden

Reputation: 12776

String.Format("{0:0.00}", 111.0);

Upvotes: 5

Guffa
Guffa

Reputation: 700382

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.

Upvotes: 3

Related Questions