15

in C#,I have a double variable price with value 10215.24. I want to show the price with comma after some digits. My expected output is 10,215.24

Cœur
  • 37,241
  • 25
  • 195
  • 267
Shyju
  • 214,206
  • 104
  • 411
  • 497
  • 2
    The answers that you have gotten suggesting .ToString("N") are good. I just want to note that the output will depend on the current CultureInfo; This is a good thing. In my system (Swedish locale), it prints "10 215,24" which makes sense to me, as "10,215.24" makes sense to you. Just keep in mind that you might run into trouble if you try to parse this string *back to a DateTime*. – Fredrik Mörk Jul 17 '09 at 12:43
  • @Fredrik: Why would he parse it back to *DateTime*? – Chris Dunaway Jul 17 '09 at 13:24
  • His brain probably switched gears mid sentence :p – Svish Jan 06 '10 at 12:47
  • As a side note, you shouldn't save price values as double, but rather as decimal types. Saving prices as double can cause a variety of rounding bugs when calculating prices. (You can read more about it here: http://stackoverflow.com/questions/1165761/decimal-vs-double-which-one-should-i-use-and-when) – gillyb Jul 06 '14 at 07:29

6 Answers6

34
myPrice.ToString("N2");

depending on what you want, you may also wish to display the currency symbol:

myPrice.ToString("C2");

(The number after the C or N indicates how many decimals should be used). (C formats the number as a currency string, which includes a currency symbol)

To be completely politically correct, you can also specify the CultureInfo that should be used.

WonderWorker
  • 8,539
  • 4
  • 63
  • 74
Frederik Gheysels
  • 56,135
  • 11
  • 101
  • 154
21

I think this should do it:

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

If you don't want the currency symbol, then just do this:

String.Format("{0:N2}", doubleVar);
Eric Petroelje
  • 59,820
  • 9
  • 127
  • 177
6

As a side note, I would recommend looking into the Decimal type for currency. It avoids the rounding errors that plague floats, but unlike Integer, it can have digits after the decimal point.

Steven Sudit
  • 19,391
  • 1
  • 51
  • 53
  • Indeed. Decimals should be used for things where precision matters. – Frederik Gheysels Jul 17 '09 at 12:45
  • 1
    In certain financial applications, it's flatly illegal to use floats because all rounding must be done in a prescribed manner. The alternative where a Decimal or BigNumbers type of class is unavailable is to use integers to store fractions of a currency unit. So, for example, we could count pennies or fractions thereof. – Steven Sudit Jul 17 '09 at 13:41
  • Isn't it true that floating point errors are isolated to Intel chips and compatibles? – WonderWorker Mar 05 '14 at 16:12
  • @Knickerless-Noggins No. The issue isn't actual errors due to bugs (as are occasionally found on certain processors), it's that all floating point operations are imprecise and involve rounding. – Steven Sudit Apr 25 '14 at 07:18
  • @Steven Sudit Sometimes you get the correct answer and sometimes you don't. The imprecision is introduced due to the FPU not handling recurring numbers. The bug is that the chip will round .9recurring down to .9999999 instead of rounding to 1 every once in a while, thus producing imprecision. It's a well known bug, but is still a bug. It is not found in all FPUs either, but is probably maintained for legacy support. Maybe it's plain old anarchy. Since I'm not the engineer responsible, we can only speculate on the reasons why. – WonderWorker Apr 29 '14 at 09:18
5

Look into format strings, specifically "C" or "N".

double price = 1234.25;
string FormattedPrice = price.ToString("N"); // 1,234.25
Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
3

This might help

    String.Format("{#,##0.00}", 1243.50); // Outputs “1,243.50″

    String.Format("{0:$#,##0.00;($#,##0.00);Zero}", 1243.50); // Outputs “$1,243.50″ 

    String.Format("{0:$#,##0.00;($#,##0.00);Zero}", -1243.50); // Outputs “($1,243.50)″ 

    String.Format("{0:$#,##0.00;($#,##0.00);Zero}", 0); // Outputs “Zero″ 
Nikita Leshchev
  • 1,784
  • 2
  • 14
  • 26
Thunder
  • 10,366
  • 25
  • 84
  • 114
0

The one you want is "N2".

Here is an example:

double dPrice = 29.999988887777666655554444333322221111; 
string sPrice = "£" + dPrice.ToString("N2"); 

You might even like this:

string sPrice = "";

if(dPrice < 1)
{
    sPrice = ((int)(dPrice * 100)) + "p";

} else
{
    sPrice = "£" + dPrice.ToString("N2");

} 

which condenses nicely to this:

string sPrice = dPrice < 1 ? ((int)(dPrice * 100)).ToString("N0") + "p" : "£" + dPrice.ToString("N2"); 

Further reading at msdn.microsoft.com/en-us/library/fht0f5be.aspx for various other types of formatting

WonderWorker
  • 8,539
  • 4
  • 63
  • 74