How do I format a double to currency rounded to the nearest dollar?

89,242

Solution 1

First - don't keep currency in a double - use a decimal instead. Every time. Then use "C0" as the format specifier:

decimal numba = 5212.6312M;
string s = numba.ToString("C0");

Solution 2

This should do the job:

String.Format("{0:C0}", Convert.ToInt32(numba))

The number after the C specifies the number of decimal places to include.

I suspect you really want to be using the decimal type for storing such numbers however.

Solution 3

Console.WriteLine(numba.ToString("C0"));

Solution 4

 decimal value = 0.00M;
        value = Convert.ToDecimal(12345.12345);
        Console.WriteLine(".ToString(\"C\") Formates With Currency $ Sign");
        Console.WriteLine(value.ToString("C"));
        //OutPut : $12345.12
        Console.WriteLine(value.ToString("C1"));
        //OutPut : $12345.1
        Console.WriteLine(value.ToString("C2"));
        //OutPut : $12345.12
        Console.WriteLine(value.ToString("C3"));
        //OutPut : $12345.123
        Console.WriteLine(value.ToString("C4"));
        //OutPut : $12345.1235
        Console.WriteLine(value.ToString("C5"));
        //OutPut : $12345.12345
        Console.WriteLine(value.ToString("C6"));
        //OutPut : $12345.123450

click to see Console Out Put screen

Hope this may Help you...

Thanks. :)

Solution 5

I think the right way to achieve your goal is with this:

Thread.CurrentThread.CurrentCulture.NumberFormat.CurrencyDecimalDigits = 0;

and only then you should do the Format call:

String.Format("{0:C0}", numba) 
Share:
89,242
spilliton
Author by

spilliton

Updated on July 08, 2022

Comments

  • spilliton
    spilliton almost 2 years

    Right now I have

    double numba = 5212.6312
    String.Format("{0:C}", Convert.ToInt32(numba) )
    

    This will give me

    $5,213.00
    

    but I don't want the ".00".

    I know I can just drop the last three characters of the string every time to achieve the effect, but seems like there should be an easier way.

  • workmad3
    workmad3 almost 15 years
    No floating point should really be used as a currency field. An integer storing in cents/pence/lowest allowable currency unit is the only way to avoid rounding errors (and a BigInt library if you need numbers beyond a long ints range :))
  • Jacob Proffitt
    Jacob Proffitt almost 15 years
    I'll second the prohibition on using doubles for currency. The imprecision can lead to trouble and, if bad enough, audits.
  • Mark Pattison
    Mark Pattison almost 15 years
    I would just add that the prohibition on using floating-points for amounts of money is not always relevant. For example, with non-precise future projections, doubles are fine.
  • Mats Fredriksson
    Mats Fredriksson almost 15 years
    Don't really agree here. Most investment banks store their values doubles. As long as you know what you are doing and know about cancellations and precision issues it should be fine.
  • geedubb
    geedubb about 9 years
    Probably worth pointing out that the performance of doubles is also much better than decimals in .NET
  • Marc Gravell
    Marc Gravell about 9 years
    @geedubb while that is true, it usually doesn't really matter how fast you get to the wrong answer ;p
  • geedubb
    geedubb about 9 years
    @MarcGravell haha very true ;)