2

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

5 Answers 5

7

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.

Sign up to request clarification or add additional context in comments.

5 Comments

May want to use currency formatting? Why would you not want to use currency formatting?
@Aaron: for interchange you might not want the symbol (or use something like ISO currency codes). And of course not al doubles are amounts of currency (the $ could be a field separator) -- you don't say you are formatting currency. (PS. better to use decimal for currency amounts to avoid rounding problems.)
@Richard My comment was when @Mark's verbiage was different...one thing that makes tracking comments difficult in the SO world...
@Aaron: Yes, I have updated my answer to make it clearer. Thanks for your comment.
+1 for recommending decimal over double. I've worked on a financial application in .NET for almost 6 years now, and decimal is almost always better than double. Double is more desired when calculations need to be done very quickly (such as in a video game).
5

Use currency formatting:

String.Format("{0:C}", money);

1 Comment

This assumes that the current culture uses $.
3
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)

Comments

2
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"

1 Comment

This doesn't work. It has the same problem that the OPs original suggestion has.
0
String.Format("${0:#,0.#}",...); 


should do it.
See Custom Numeric Format Strings

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.