views:

1755

answers:

5

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.

+6  A: 

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
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
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
I'll second the prohibition on using doubles for currency. The imprecision can lead to trouble and, if bad enough, audits.
Jacob Proffitt
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
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
+2  A: 
Console.WriteLine(numba.ToString("C0"));
RedFilter
+2  A: 

Try {0:C0}

Joe
+2  A: 

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.aspx) type for storing such numbers however.

Noldorin
+1  A: 

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