tickwave
tickwave

Reputation: 3445

Show 2 decimal only if the decimal is not .00

So, I have a requirements to show decimal only if the value of the decimal itself is not .00

I'm using this currently .ToString("#,###,##0.##") but I have issue below

Expectation

1000 -> 1,000
5522.13 -> 5,522.13
55.1 -> 55.10

Reality

1000 -> 1,000
5522.13 -> 5,522.13
55.1 -> 55.1

The part 55.1 will only show one decimal instead of 55.10. Any help will be appreciated

Upvotes: 0

Views: 387

Answers (3)

NajiMakhoul
NajiMakhoul

Reputation: 1716

use ToString("#,#.#0")

var numbers = new [] { 1000.0, 5522.13, 55.1 ,15003.2};

foreach (var number in numbers)
{
    var formatted = number.ToString("#,#.#0");
    Console.WriteLine(formatted);
}   

result:

1,000.00
5,522.13
55.10
15,003.20

.NET Fiddle

Upvotes: 0

Enigmativity
Enigmativity

Reputation: 117064

It seems to me that a simple .Replace(".00", "") might work for you.

var numbers = new [] { 1000.0, 5522.13, 55.1 };

foreach (var number in numbers)
{
    var formatted = number.ToString("#,###,##0.00").Replace($"{CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator}00", "");
    Console.WriteLine(formatted);
}

Or:

Dim numbers = {1000.0, 5522.13, 55.1}

For Each number In numbers
    Dim formatted = number.ToString("#,###,##0.00").Replace($"{CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator}00", "")
    Console.WriteLine(formatted)
Next

Those both give me:

1,000
5,522.13
55.10

Upvotes: 0

jmcilhinney
jmcilhinney

Reputation: 54417

There's no way to do that with a single format specifier. You're going to have to use some conditional logic and format differently in the two cases. There would be various ways to do that but here's one that's fairly simple:

Imports System.Globalization

Module Module1

    Sub Main()
        Console.WriteLine(FormatNumber(1000.0))
        Console.WriteLine(FormatNumber(5522.13))
        Console.WriteLine(FormatNumber(55.1))

        Console.ReadLine()
    End Sub

    Private Function FormatNumber(number As Double) As String
        Dim temp = number.ToString()

        Return If(temp.Contains(CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator),
                  number.ToString("n2"),
                  number.ToString("n0"))
    End Function

End Module

Note that I believe that that should work for all values that do not need to be represented using scientific notation, which should be fine for most people. If you have a very large value then ToString may output scientific notation that contains a decimal separator, even though the actual number has no fractional part.

Another option, which tests the number rather than the string representation of the number, is as below:

Private Function FormatNumber(number As Double) As String
    Return If(Math.Round(number) = number,
              number.ToString("n0"),
              number.ToString("n2"))
End Function

Note that the format specifiers are reversed in that second case, because the test is for the absence of a decimal point where the first example tests for the presence of a decimal point.

Upvotes: 2

Related Questions