Home > Mobile >  Format Decimal so it shows 123,456.78 but to not show 00.00 when 0
Format Decimal so it shows 123,456.78 but to not show 00.00 when 0

Time:11-23

I have Decimal values that have 123456.78 and I want to format it to be 123,456.78

for this I'm doing @$"{value:0,0.00}" but I don't know if it's the best approach, but at first glance is that when the decimal has 0, the value shown is 00.00 which looks weird. Is there a suggestion to do it better than with the interpolation. I'm storing monetary amounts, but different currencies so using currency wouldn't be the best (as it comes from a float value from database -I know, bad- and a separate field specifies the currency code for it).

so, to put it short, what to do best to express decimal with 123,456.78 format and to show 0.00 for 0 values.

Thank you.

CodePudding user response:

Use # to add placeholders that are not used if the digit is a leading zero:

foreach (var value in new [] {123456.78 , 0})
{
    Console.WriteLine($"{value:#,0.00}");
}

output:

123,456.78
0.00
  •  Tags:  
  • c#
  • Related