If you want to get exactly this result, you can not use the formatting of numerical data, since it does not format 123 as 123.0 . You should consider values as strings to preserve trailing zero.
This gives you exactly the result you requested:
string[] numbers = { "0", "0.0002", "0.531", "2.42", "12.5", "123.0", "123172" }; foreach (string number in numbers) { int pos = number.IndexOf('.'); if (pos == -1) pos = number.Length; Console.WriteLine(new String(' ', 6 - pos) + number); }
Output:
0 0.0002 0.531 2.42 12.5 123.0 123172
Guffa
source share