9

To meet a specification I need to format a decimal value with the rule:

  • All values must have at least two decimal places and at most six.

Example:

Value        Formatted
1            1.00
1.1          1.10
1.1234       1.1234
1.123456     1.123456
1.12345678   1.123456

I guess I'll end using a condition, but I wonder if there is a format string which can do this.

MiguelM
  • 197
  • 2
  • 9
  • similar question: https://stackoverflow.com/questions/3104615/best-way-to-display-decimal-without-trailing-zeroes – Flimtix Mar 18 '22 at 14:33

1 Answers1

18

Have you tried:

.ToString("0.00####");

Barry Kaye
  • 7,682
  • 6
  • 42
  • 64