I am currently using the following to format a decimal as a currency.
double value = 12345.6789;
Console.WriteLine(value.ToString("C", CultureInfo.CurrentCulture));
// Result: $12,345.68
Which is the behaviour I want, except when the decimals are 0, as in double value = 12345.00;
Then the result should be $12,345
I've tried variations using #
like .ToString("C.##"
etc. but I can't get it to work as I'd like.
Using decimal
, you could determine which format string to use:
//decimal value = 12345.6789m;
decimal value = 12345.0000m;
var formatString = Decimal.Truncate(value) == value ? "C0" : "C";
Console.WriteLine(value.ToString(formatString, CultureInfo.CurrentCulture));
这样的东西怎么样.ToString("0.##")
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.