72

How do I format a decimal value to a string with a single digit after the comma/dot and leading spaces for values less than 100?

For example, a decimal value of 12.3456 should be output as " 12.3" with single leading space. 10.011 would be " 10.0". 123.123 is "123.1"

I'm looking for a solution, that works with standard/custom string formatting, i.e.

decimal value = 12.345456;
Console.Write("{0:magic}", value); // 'magic' would be a fancy pattern.
Jakob Gade
  • 12,319
  • 15
  • 70
  • 118

7 Answers7

107

This pattern {0,5:###.0} should work:

string.Format("{0,5:###.0}", 12.3456) //Output  " 12.3"
string.Format("{0,5:###.0}", 10.011)  //Output  " 10.0" 
string.Format("{0,5:###.0}", 123.123) //Output  "123.1"
string.Format("{0,5:###.0}", 1.123)   //Output  "  1.1"
string.Format("{0,5:###.0}", 1234.123)//Output "1234.1"
nemesv
  • 138,284
  • 16
  • 416
  • 359
52

Another one with string interpolation (C# 6+):

double x = 123.456;
$"{x,15:N4}"// left pad with spaces to 15 total, numeric with fixed 4 decimals

Expression returns: " 123.4560"

Freek Wiekmeijer
  • 4,556
  • 30
  • 37
12
value.ToString("N1");

Change the number for more decimal places.

EDIT: Missed the padding bit

value.ToString("N1").PadLeft(1);
Taz
  • 1,235
  • 9
  • 16
  • Thanks. But the use of PadLeft won't work in my case, the string with the {0:...} placeholder is going to contain more text than just the decimal value. – Jakob Gade Nov 28 '11 at 08:55
  • 2
    @Taz: You have to write `PadLeft(5)'. The parameter is the total length of the string to be padded. – Jan Nov 28 '11 at 09:01
  • @Jakob: Why can't you use `string.Format()`? – Jan Nov 28 '11 at 09:02
  • So whats the problem with PadLeft? – Jan Nov 28 '11 at 09:06
  • `PadLeft(value.ToString().Length + 1)` then I guess ;) Ignoring the issue of more stuff in the string of course. You cant just split it up and then combine them back together to print them? – Taz Nov 28 '11 at 09:06
  • 2
    @Taz: No. Simply `PadLeft(5)` – Jan Nov 28 '11 at 09:14
  • 4
    Thanks! If I can, I avoid string.format. This here is much easier to maintain later without all those cryptic string codes. – Bitterblue Jul 14 '14 at 09:06
3

Many good answers, but this is what I use the most (c# 6+):

Debug.WriteLine($"{height,6:##0.00}");
//if height is 1.23 =>   "  1.23"
//if height is 0.23 =>   "  0.23"
//if height is 123.23 => "123.23"

Brian Hong
  • 930
  • 11
  • 15
0

All above solution will do rounding of decimal, just in case somebody is searching for solution without rounding

decimal dValue = Math.Truncate(1.199999 * 100) / 100;
dValue .ToString("0.00");//output 1.99
Anshul Nigam
  • 1,608
  • 1
  • 12
  • 26
0

Note the "." could be a "," depending on Region settings, when using string.Format.

string.Format("{0,5:###.0}", 0.9)     // Output  "   .9"
string.Format("{0,5:##0.0}", 0.9)     // Output  "  0.9"

I ended up using this:

string String_SetRPM = $"{Values_SetRPM,5:##0}";
// Prints for example "    0", " 3000", and "24000"

string String_Amps = $"{(Values_Amps * 0.1),5:##0.0}";
// Print for example "  2.3"

Thanks a lot!

Jack
  • 151
  • 4
  • 12
0

Actually the accepted answer will produce " .1" in case of 0.123, which might be unexpected.

Therefore I'd prefer using 0.0 instead of ###.0 as number format. But it depends on your needs (see bottom of my comment).

Examples:

string.Format("{0,5:0.0}", 199.34) // Output "199.3"
string.Format("{0,5:0.0}",  19.34) // Output " 19.3"
string.Format("{0,5:0.0}",   0.34) // Output "  0.3"

Explaining {0,5:0.0}

Pattern: {{index},{padding}:{numberFormat}}

  • {index}: zero based index of the parameter to be formatted out of a list of parameters passed to string.Format("format", param0, param1, param2...)
  • {padding}: number of leading spaces to pad
  • {numberFormat}: the actual number format to apply on the number

Padding documentation: https://learn.microsoft.com/en-us/dotnet/standard/base-types/how-to-pad-a-number-with-leading-zeros

Custom Number Format doucmentation: https://learn.microsoft.com/en-us/dotnet/standard/base-types/custom-numeric-format-strings

It is worth to compare the difference between 0 and # decimal specifier:

0 decimal specifier: https://learn.microsoft.com/en-us/dotnet/standard/base-types/custom-numeric-format-strings#Specifier0

# decimal specifier: https://learn.microsoft.com/en-us/dotnet/standard/base-types/custom-numeric-format-strings#SpecifierD