The "C" (or currency) format specifier is used to convert a number to a string representing a currency amount. Let us see an example. double value = 139.87; Now to display the above number until three decimal places, use (“C3”) currency format specifier.
If the value to be formatted has more than the specified or default number of decimal places, the fractional value is rounded in the result string. If the value to the right of the number of specified decimal places is 5 or greater, the last digit in the result string is rounded away from zero.
string dateString = int. Parse("20120321"). ToString("yyyy/MM/dd");
Specify you want zero decimal places:
String.Format("{0:C0}", item.DonationAmount)
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");
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. :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With