Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Converting numbers to strings, right justified 2

Status
Not open for further replies.

CaKiwi

Programmer
Apr 8, 2001
1,294
US
I want to know a good way of converting integer or real variables into strings right justified with leading spaces, e.g.

1
10
100
1000

2.54
25.40
254.00

Thanks in advance CaKiwi

"I love mankind, it's people I can't stand" - Linus Van Pelt
 
What are you using to display the string?

If it's a textbox for example just set alignment property to 1 - Right Justify
________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'People who live in windowed environments shouldn't cast pointers.'
 

irstuff -
I can't find any information on the text function in my documentation.

johnwm -
I am trying to display several numbers on each line of a textbox and I want them to line up. CaKiwi

"I love mankind, it's people I can't stand" - Linus Van Pelt
 
To right justify a number with leading spaces, you could try the following:
Code:
Text = Right(Space(Length) & Trim(Value), Length)
If you want to pre-format the number using a typical mask, then you can also do the following:
Code:
Text = Right(Space(Length) & Format(Value, Mask), Length)
Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 

Thanks CajunCenturion.

I have changed the subroutine I wrote (which used a "000" format followed by a while loop to replace leading zeroes with spaces) to use the method you suggested. I am surprised that there is not a format mask that will handle it. I couldn't find anywhere in the documentation explaining format masks, only some examples under the format statement. Am I missing something? CaKiwi

"I love mankind, it's people I can't stand" - Linus Van Pelt
 
If there is a way to right justify with leading spaces using the Format function, I am not aware of it. Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 

Just to add, you could also write it as:

Format$(sNumber,String(Length-Len(Value)," ") & Value)
Or
?Format$(sNumber,Space(Length-Len(Value)) & Value)
 
Hi,

This is probally a little over the top :


Function FormatRightJustify(Expression As String, Length As Long) As String
FormatRightJustify = Format(Expression, String(Length, "@"))
End Function

Debug.Print FormatRightJustify(CStr(Now), Len(CStr(Now)) + 5)

Rich.
____ Programmers are tools for converting caffeine into code - BSRF
 
Use this to right justify with the format command:

Text = Format$(1.5, String$(10, "@")) Swi
 
Nice one Swi !
________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'People who live in windowed environments shouldn't cast pointers.'
 
Do you want right justified like this?

1
10
100
1000
10000

What I see on your post is centered justified?

Bill
 
so is yours bill [smile]
actually they want this,
[tt]
1
10
100
1000
10000
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top