views:

156

answers:

4

Hi, I have these numbers:

5.25
10251.35
5

and I want them to be formatted with groups and always with 2 decimals ,XX

this is what .ToString("N2") does:

5,25
10.251,35
5

How can i make the '5' look like 5,00 too ? And for multiple cultures of course (en: 5.0, de: 5,0 ...)

In fact this question has no sense, N2 should do it at all costs. The problem was in my WebServer.

+2  A: 

Isn't this working for you:

decimal d = 5m;
string formatted = d.ToString("N2");

This will use the current culture, but you could specify one:

string formatted = d.ToString("N2", new CultureInfo("fr-FR"));
Darin Dimitrov
That's what I'm doing. But it does not display 12.510,00. It only displays 12.510
PaN1C_Showt1Me
For what input?
Darin Dimitrov
`12510m.ToString("N2")` returns `12,510.00` for en-US.
Darin Dimitrov
if it does.. something must be very wrong.. cause when i paste this into my code: 12510m.ToString("N2") it simply prints 12,510
PaN1C_Showt1Me
What culture are you using?
Darin Dimitrov
i set the culture settings for en-US
PaN1C_Showt1Me
OK but if you say that it should output it with decimals then i accept it. There problem must be somewhere else.. thank you
PaN1C_Showt1Me
How about `12510m.ToString("N2", CultureInfo.InvariantCulture)`?
Darin Dimitrov
Well at least that's what the documentation states (http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx)
Darin Dimitrov
Yes.. I read it of course. I restarted my WebServer and it suddenly works.. I can't believe that. 10,251.355.004.00
PaN1C_Showt1Me
+2  A: 

.ToString("{0:#,0.00}")

Note: although this uses US grouping & decimal symbols, the result will be locale aware, assuming that either your current user locale is set correctly or you pass a locale into ToString().

Gary McGill
Your solution works too of course. +1 point
PaN1C_Showt1Me
A: 

IIRC:

toString("0:0.00");

MSDN

PoweRoy
A: 

With the format you have (Fixed-point), you should use:

.ToString("F2");

You have all the format here: MSDN

Procule
In fact, I'd like to have a combination N with F, not only F2
PaN1C_Showt1Me