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

C#FormattingRoundingCurrency

C# Problem Overview


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.

C# Solutions


Solution 1 - C#

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 - C#

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 - C#

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

Solution 4 - C#

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) 

Solution 5 - C#

 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 6 - C#

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionspillitonView Question on Stackoverflow
Solution 1 - C#Marc GravellView Answer on Stackoverflow
Solution 2 - C#NoldorinView Answer on Stackoverflow
Solution 3 - C#D'Arcy RittichView Answer on Stackoverflow
Solution 4 - C#PejvanView Answer on Stackoverflow
Solution 5 - C#Bhanu PratapView Answer on Stackoverflow
Solution 6 - C#Rodrigo ReisView Answer on Stackoverflow