String.Format way to format Currency without Cents
I'm displaying currency using the current method
String.Format("{0:C}", item.DonationAmount)
Which outputs like $10.00
We will be dealing with large dollar amounts, and no cents. We would like the currency to display as $10 without the decimal or zeroes. How can I do this? Removing the currency format makes the number display like 10.0000 :( thanks in advance.
Solution 1:
Specify you want zero decimal places:
String.Format("{0:C0}", item.DonationAmount)
Solution 2:
if item.DonationAmount
is a non-nullable
decimal
then you could just do:
item.DonationAmount.ToString("c0");
if item.DonationAmount
is a nullable
decimal
that you checked has a value then you could do:
item.DonationAmount.Value.ToString("c0");
or in a newer version of C#
, which doesn't require you to check if it has a value:
item.DonationAmount?.ToString("c0");
Solution 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
Console.WriteLine();
Console.WriteLine(".ToString(\"F\") Formates With out Currency Sign");
Console.WriteLine(value.ToString("F"));
//OutPut : 12345.12
Console.WriteLine(value.ToString("F1"));
//OutPut : 12345.1
Console.WriteLine(value.ToString("F2"));
//OutPut : 12345.12
Console.WriteLine(value.ToString("F3"));
//OutPut : 12345.123
Console.WriteLine(value.ToString("F4"));
//OutPut : 12345.1235
Console.WriteLine(value.ToString("F5"));
//OutPut : 12345.12345
Console.WriteLine(value.ToString("F6"));
//OutPut : 12345.123450
Console.WriteLine();
Console.WriteLine(".ToString(\"N\") Formates With out Currency Sign");
Console.WriteLine(value.ToString("N"));
//OutPut : 12,345.12
Console.WriteLine(value.ToString("N1"));
//OutPut : 12,345.1
Console.WriteLine(value.ToString("N2"));
//OutPut : 12,345.12
Console.WriteLine(value.ToString("N3"));
//OutPut : 12,345.123
Console.WriteLine(value.ToString("N4"));
//OutPut : 12,345.1235
Console.WriteLine(value.ToString("N5"));
//OutPut : 12,345.12345
Console.WriteLine(value.ToString("N6"));
//OutPut : 12,345.123450
Console.WriteLine();
Console.WriteLine(".ToString(\"P\") Formates With Percentage (i.e multiply by 100)");
Console.WriteLine(value.ToString("P"));
//OutPut : 1,2345,12.35%
Console.WriteLine(value.ToString("P1"));
//OutPut : 1,2345,12.3%
Console.WriteLine(value.ToString("P2"));
//OutPut : 12345,12.35%
Console.WriteLine(value.ToString("P3"));
//OutPut : 12345,12.345%
Console.WriteLine(value.ToString("P4"));
//OutPut : 12345,12.3450%
Console.WriteLine(value.ToString("P5"));
//OutPut : 12345,12.34500%
Console.WriteLine(value.ToString("P6"));
//OutPut : 12345,12.345000%
Console.Read();
Click to see Console Out Put Screen
Hope this may Help you...
Thanks. :)