64

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.

Bernard Vander Beken
  • 4,315
  • 5
  • 46
  • 70
spilliton
  • 3,631
  • 4
  • 33
  • 35

6 Answers6

96

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");
Marc Gravell
  • 927,783
  • 236
  • 2,422
  • 2,784
  • 1
    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 :)) – workmad3 May 20 '09 at 20:39
  • 4
    An integer has similar rounding issues as decimal... decimal is essentially a largish integer with a precision specifier. Most things you can do with int you can do with decimal, plus decimal is 96-bits (rather that 32 or 64 for int and long). Most currencies fit into 96 bits... – Marc Gravell May 20 '09 at 20:43
  • 1
    I'll second the prohibition on using doubles for currency. The imprecision can lead to trouble and, if bad enough, audits. – Jacob Proffitt May 20 '09 at 23:24
  • 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. – Mark Pattison Jun 16 '09 at 10:45
  • 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. – Mats Fredriksson Jun 16 '09 at 10:48
  • 2
    Probably worth pointing out that the performance of doubles is also much better than decimals in .NET – geedubb Feb 12 '15 at 14:21
  • 9
    @geedubb while that is *true*, it usually doesn't really matter how fast you get to the wrong answer ;p – Marc Gravell Feb 12 '15 at 14:40
  • 1
    @MarcGravell haha very true ;) – geedubb Feb 12 '15 at 15:21
24

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.

Noldorin
  • 134,265
  • 53
  • 250
  • 293
5
Console.WriteLine(numba.ToString("C0"));
D'Arcy Rittich
  • 153,827
  • 35
  • 271
  • 277
3

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) 
Pejvan
  • 692
  • 5
  • 8
3
 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. :)

Bhanu Pratap
  • 1,225
  • 12
  • 14
0

simple: numba.ToString("C2")

more @ http://msdn.microsoft.com/pt-br/library/dwhawy9k(v=vs.110).aspx#CFormatString

Rodrigo Reis
  • 1,014
  • 10
  • 20