Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Format Numeric Value with Leading Spaces

Tags:

vb.net

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"?

like image 614
Brian Hooper Avatar asked Dec 09 '25 20:12

Brian Hooper


1 Answers

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

String.Format("{0,6:####.#}", number)
like image 69
Guffa Avatar answered Dec 12 '25 19:12

Guffa



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!