Jakob Gade
Jakob Gade

Reputation: 12419

Format decimal value to string with leading spaces

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.

Upvotes: 78

Views: 79657

Answers (7)

Krisztian Kaszas
Krisztian Kaszas

Reputation: 1

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

Upvotes: 0

Jack
Jack

Reputation: 161

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!

Upvotes: 0

Brian Hong
Brian Hong

Reputation: 1084

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"

Upvotes: 5

Freek Wiekmeijer
Freek Wiekmeijer

Reputation: 4940

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"

Upvotes: 58

Anshul Nigam
Anshul Nigam

Reputation: 1628

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

Upvotes: 0

nemesv
nemesv

Reputation: 139758

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"

Upvotes: 116

Taz
Taz

Reputation: 1303

value.ToString("N1");

Change the number for more decimal places.

EDIT: Missed the padding bit

value.ToString("N1").PadLeft(1);

Upvotes: 15

Related Questions