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

I have now

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

It will give me

 $5,213.00 

but I do not want ".00".

I know that I can simply discard the last three characters of the string each time to achieve the effect, but there seems to be an easier way.

+48
c # formatting rounding currency
May 20, '09 at 20:35
source share
6 answers

First - do not save the currency in double - use decimal instead. Everytime. Then use "C0" as the format specifier:

 decimal numba = 5212.6312M; string s = numba.ToString("C0"); 
+78
May 20 '09 at 20:36
source share
— -

This should complete the task:

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

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

I suspect you really want to use decimal to store such numbers.

+23
May 20, '09 at 20:41
source share
 Console.WriteLine(numba.ToString("C0")); 
+5
May 20 '09 at 20:39
source share

I think the right way to achieve your goal is as follows:

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

and only then should you call Format:

 String.Format("{0:C0}", numba) 
+3
Jun 16 '09 at 10:31
source share
  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 the Out Put Console screen

Hope this can help you ...

Thank.:)

+3
Jul 25 '16 at 11:00
source share
0
Sep 26 '14 at 2:39 on
source share



All Articles