Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove 0s from the end of a decimal value [duplicate]

Tags:

c#

decimal

I have a decimal value that has a variable number of digits after the ., for example:

0.0030
0.0310
0.0001
1.1200

How can I write a dynamic function that removes 0 in the end of the decimal?

like image 416
Fifty Avatar asked Jan 21 '10 13:01

Fifty


2 Answers

You can also modify the decimal itself so that any ToString() will give you what you want (more details in my answer here) :

public static decimal Normalize(decimal value)
{
    return value/1.000000000000000000000000000000000m;
}
like image 65
Thomas Materna Avatar answered Oct 04 '22 22:10

Thomas Materna


string.Format("{0:0.#####}", 0.0030)

or

var money=1.3000m;
money.ToString("0.#####");

For future reference I recommend the .NET Format String Quick Reference by John Sheehan.

like image 31
Jonas Elfström Avatar answered Oct 04 '22 20:10

Jonas Elfström