Twiebie
Twiebie

Reputation: 412

Format String with fixed length with leading spaces

I need to format a string for a fixed length. I tried to use this format function from msdn

Dim i as Decimal
i = 123,12
MsgBox(Format(i,"######.##"))

Output 123.12

But the Output I want is with the leading spaces. ( the underscore should be a space char )

Output ___123.12

Upvotes: 2

Views: 7001

Answers (5)

StuckySB
StuckySB

Reputation: 1

'If i want leading spaces in fixed number format minimum 4 digits with leading spaces up to you to validate number is in range and doesn't get truncated. dim string as string dim num as integer

num = 999

tstr = right(" " & format(num, "#0"),4)

Upvotes: 0

Jim Mack
Jim Mack

Reputation: 1144

Before the arrival of Format, the canonical way to do this was:

Result$ = Right$(Space$(9) & Incoming$, 9)

...which you can generalize by replacing "9" with a variable.

This works for leading zeroes as well, using String$("0", n) instead of Space$(n).

Upvotes: 1

Kidus
Kidus

Reputation: 1843

Add the spaces in the format syntax like this and it'll work.

Format(12.323, "  ##.##")

Upvotes: 4

BabyProgrammer
BabyProgrammer

Reputation: 115

Have you tried the Rset command?

x = (Format$(123.5, "$##0.00"))

Print "x" & x & "x"

RSet x = (Format$(1.5, "$##0.00"))

Print "x" & x & "x"

Output :

x$123.50x

x $1.50x

Upvotes: -1

BabyProgrammer
BabyProgrammer

Reputation: 115

string s = "String goes here";

string line1 = String.Format("{0,27}", s);

string line2 = String.Format("{0,-27}", String.Format("{0," + ((27 + s.Length) / 2).ToString() + "}", s));

string line3 = String.Format("{0,-27}", s);

Upvotes: -2

Related Questions