Fernando Meneses Gomes
Fernando Meneses Gomes

Reputation: 877

Format decimal in C# with at least 2 decimal places

Is there a display formatter that will output decimals as these string representations in C# without doing any rounding?

The decimal may have 2 decimal places, but if it has more precision it should be included in the resultant string and not rounded off.

Examples:

decimal  -> string
20       -> 20,00
20.00    -> 20,00
20.5     -> 20,50
20.5000  -> 20,50
20.125   -> 20,125
20.12500 -> 20,125

Thanks in advance

Upvotes: 11

Views: 3964

Answers (1)

Henk Holterman
Henk Holterman

Reputation: 273824

When you have a reasonable upper limit for the maximum number of digits:

 ToString("0.00#######")

will handle all your examples but not 1.23456789012345M

Upvotes: 18

Related Questions