Reputation: 9961
I am using next code to format double value:
String.Format("${0:0,0.#}",...);
It working great, but when numbers are less than 10, I got problem. Numbers are displayed as $03, $06 for example.
Please advise me correct string to have a double number in next format ddd,ddd,ddd,ddd.dd
Upvotes: 2
Views: 2115
Reputation: 7400
String.Format("${0:#,0.#}",...);
should do it.
See Custom Numeric Format Strings
Upvotes: 0
Reputation: 838376
Try this instead:
string result = string.Format("${0:#,##0.00}", d);
If your double represents a currency you should use:
string result = string.Format(CultureInfo.GetCultureInfo("en-US"), "{0:c}", d);
Note that if you omit the CultureInfo.InvariantCulture
it could display using something other than $
on some computers. For example on my computer string.Format("{0:c}", d)
gives 2,00 kr
which might not be what you wanted.
In your example you don't actually need to use string.Format
at all. You could use this instead:
string s = d.ToString("c", CultureInfo.GetCultureInfo("en-US"));
As well as being clearer and more concise, it also has the advantage of avoiding boxing. Of course if your format string is more complex than in your example then it would make sense to use string.Format
.
And as a final remark I'd recommend against using doubles to store currency. A decimal type is probably more appropriate.
Upvotes: 7
Reputation: 12596
String.Format("{0:C}", myDecimal);
or myDecimal.ToString("C");
will display to two decimal places, include the comma separator and include the dollar sign (based on culture settings) in one fell swoop. If you want it to go to 1 or more than 2 decimal places, include a number after the C (i.e. C3)
Upvotes: 3
Reputation:
Digits after decimal point
// just two decimal places
String.Format("{0:0.00}", 123.4567); // "123.46"
String.Format("{0:0.00}", 123.4); // "123.40"
String.Format("{0:0.00}", 123.0); // "123.00"
// max. two decimal places
String.Format("{0:0.##}", 123.4567); // "123.46"
String.Format("{0:0.##}", 123.4); // "123.4"
String.Format("{0:0.##}", 123.0); // "123"
// at least two digits before decimal point
String.Format("{0:00.0}", 123.4567); // "123.5"
String.Format("{0:00.0}", 23.4567); // "23.5"
String.Format("{0:00.0}", 3.4567); // "03.5"
String.Format("{0:00.0}", -3.4567); // "-03.5"
Thousands separator
String.Format("{0:0,0.0}", 12345.67); // "12,345.7"
String.Format("{0:0,0}", 12345.67); // "12,346"
Zero
Following code shows how can be formatted a zero (of double type).
String.Format("{0:0.0}", 0.0); // "0.0"
String.Format("{0:0.#}", 0.0); // "0"
String.Format("{0:#.0}", 0.0); // ".0"
String.Format("{0:#.#}", 0.0); // ""
Align numbers with spaces
String.Format("{0,10:0.0}", 123.4567); // " 123.5"
String.Format("{0,-10:0.0}", 123.4567); // "123.5 "
String.Format("{0,10:0.0}", -123.4567); // " -123.5"
String.Format("{0,-10:0.0}", -123.4567); // "-123.5 "
Custom formatting for negative numbers and zero
String.Format("{0:0.00;minus 0.00;zero}", 123.4567); // "123.46"
String.Format("{0:0.00;minus 0.00;zero}", -123.4567); // "minus 123.46"
String.Format("{0:0.00;minus 0.00;zero}", 0.0); // "zero"
Some funny examples
String.Format("{0:my number is 0.0}", 12.3); // "my number is 12.3"
String.Format("{0:0aaa.bbb0}", 12.3); // "12aaa.bbb3"
Upvotes: 2