matthias
matthias

Reputation: 199

remove tailing zeros in decimal

I want to remove the tailing zeros of a decimal. It is loaded from DB with precision 5.

I am using a culture where the comma (,) is the decimal point.

E.g:

I could use the General ("G") Format Specifier but i don't want the scientific notation at any point.

BR, m.

Upvotes: 0

Views: 369

Answers (3)

Guillaume
Guillaume

Reputation: 13138

You can use a custom numeric format string

decimal d = 12.45600m;
d.ToString("0.#####");

Upvotes: 3

pawciu
pawciu

Reputation: 935

Probably there is more elegant solution, but this worked on strings.

string[] decimals = {
                                "10,00050",// -> 10,0005
                                "10,00000",// -> 10
                                "0,00000",// -> 0
                                "0,00001",// -> 0,00001
                             };
        foreach (string dec in decimals)
        {
            Console.WriteLine(" {0} -> {1}", dec, decimal.Parse(dec.TrimEnd(new char[] { '0' })));
        }

Upvotes: 0

Brian Ball
Brian Ball

Reputation: 12596

Try "N", it adds commas if the number is a thousand or higher.

Upvotes: -1

Related Questions