0

Possible Duplicate:
C# How to remove 0 from the end of a decimal value
Best way to display decimal without trailing zeros in c#

I have decimal variable it has value

2.50
2.25

How can I convert it into string so I can get

2.5
2.25

Issue is 2.50 must be 2.5 in string.

Community
  • 1
  • 1
SOF User
  • 7,590
  • 22
  • 75
  • 121

2 Answers2

3
String.Format("{0:0.##}", 2.50);

You can also convert it to a string and then do String.Trim('0'); to trim the trailing zeros off.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
nmjohn
  • 1,432
  • 7
  • 16
1

You could do it like this example shows:

String.Format("{0:0.00}", 123.4567);      // "123.46"
String.Format("{0:0.00}", 123.4);         // "123.40"
String.Format("{0:0.00}", 123.0);         // "123.00"

(Basically, using the String.Format function)

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
DigCamara
  • 5,540
  • 4
  • 36
  • 47