Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Format decimal c# - Keep last zero

I have been searching for this but cannot seem to find the answer. I have the following decimals with the corresponding output that I want from String.Format

100.00 -> 100
100.50 -> 100.50
100.51 -> 100.51

My problem is that I cannot seem to find a format which will keep the 0 on the end of 100.50 as well as remove the 2 zeros from 100.

Any help is much appreciated.

EDIT For some more clarity. I have variables of type decimal, they are only ever going to be 2 decimal places. Basically I want to display 2 decimal places if they exist or none, I don't want to display one decimal place in the case of 100.50 becoming 100.5

like image 893
Peuge Avatar asked Jul 27 '12 07:07

Peuge


1 Answers

As far as I know, there is no such format. You will have to implement this manually, e.g.:

String formatString = Math.Round(myNumber) == myNumber ? 
                      "0" :     // no decimal places
                      "0.00";   // two decimal places
like image 62
Heinzi Avatar answered Oct 05 '22 15:10

Heinzi