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
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
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