Alex Gurskiy
Alex Gurskiy

Reputation: 273

String.Format() convert decimal? to ###,###,###.00 format

I'm trying to convert decimal? price to formatted price. I need such price format: 222,777,333.00 This is my code: String.Format(item.SalePrice.ToString(), "###,###,###.00"). Result: 99999.000000 Help, please.

Upvotes: 0

Views: 14698

Answers (4)

Idos
Idos

Reputation: 15320

This is how you should do it:
item.SalePrice.ToString("N2");

Upvotes: 2

Soner Gönül
Soner Gönül

Reputation: 98810

Using The numeric ("N") format specifier in a 2 precision specifier with a culture that has , as a NumberGroupSeparator and . as a NumberDecimalSeparator (like InvariantCulture) is the best way in your case.

item.SalePrice.ToString("N2", CultureInfo.InvariantCulture)

If you don't specify any culture, this method uses CurrentCulture by default and you might get different representation if your CurrentCulture has different specifiers for these properties.

Upvotes: 1

Hamza Hasan
Hamza Hasan

Reputation: 1398

Try item.SalePrice.ToString("###,###,###.00");

It works :)

Upvotes: 2

Umut Seven
Umut Seven

Reputation: 408

Try

item.SalePrice.ToString("N");

Check out this msdn article for more options in regards to Decimal.ToString().

Upvotes: 1

Related Questions