3

I am trying to format a numeric value with leading spaces, to pack the string out to a given length, thus:-

mystring = mynumber.ToString ("format-string")

but am having no luck finding the correct format string. I have seen the question here and tried the answer suggested, but it doesn't work. For 123.4,

.ToString ("####.#")        gets "123.4"
.ToString ("6:####.#")      gets "6:123.4"
.ToString ("0,6:####.#")    gets "06:0,123.4"
.ToString ("{####.#}")      gets "{123.4}"
.ToString ("{6:####.#}")    gets "{6:123.4}"
.ToString ("{0,6:####.#}")  gets "{06:0,123.4}"

Is there anything I can try to get " 123.4"?

Community
  • 1
  • 1
Brian Hooper
  • 21,544
  • 24
  • 88
  • 139

1 Answers1

8

You can't use the ToString method for that format, you need to use the String.Format method:

String.Format("{0,6:####.#}", number)
Guffa
  • 687,336
  • 108
  • 737
  • 1,005