For Programmers: Free Programming Magazines  


Home > Archive > Visual Basic > May 2004 > How do I right justify a number









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author How do I right justify a number
Thomas Scheiderich

2004-05-27, 5:31 am

How do I right justify a number for displaying on a printout or in a
textbox. These are not objects. They are variables.

I can't seem to get it to work. If I have the following format:
Format(Number, "###0.0") and numbers
.1, 100.1 and 10

I end up with
0.1
100.1
10.0
But I want to end up with:
0.1
100.1
10.0

How do I do this?

Thanks,

Tom



Bob O`Bob

2004-05-27, 5:31 am

Thomas Scheiderich wrote:
>
> How do I right justify a number for displaying on a printout or in a
> textbox. These are not objects. They are variables.
>
> I can't seem to get it to work. If I have the following format:
> Format(Number, "###0.0") and numbers
> .1, 100.1 and 10
>
> I end up with
> 0.1
> 100.1
> 10.0
> But I want to end up with:
> 0.1
> 100.1
> 10.0
>
> How do I do this?



There are lots of possibilities.
Here's one:

right$(space(6) & format(number, "###0.0"),6)

Somewhat more generic:

strFormat = "###0.0"
n = len(strformat)
strOutput = right$(space(n) & format(number, strFormat), n)



Bob
Rick Rothstein

2004-05-27, 6:30 am

> > How do I right justify a number for displaying on a printout
or in a
format:[color=darkred]
>
>
> There are lots of possibilities.
> Here's one:
>
> right$(space(6) & format(number, "###0.0"),6)
>
> Somewhat more generic:
>
> strFormat = "###0.0"
> n = len(strformat)
> strOutput = right$(space(n) & format(number, strFormat), n)


Or....

strFormat = "0.0" ' you don't need the all those # signs
n = 6 ' size of field to right justify in
strOutput = Format$(Format$(number, strFormat), String$(n, "@"))

The last line is the generic form of this (for n=6):

strOutput = Format$(Format$(number, strFormat), "@@@@@@")

where the inner Format statement formats the number to one decimal place
and the outer formats that to a right-justified string.

Rick - MVP

J French

2004-05-27, 8:30 am

On Thu, 27 May 2004 01:31:47 -0700, "Thomas Scheiderich"
<tfs@deltanet.com> wrote:

> How do I right justify a number for displaying on a printout or in a
>textbox. These are not objects. They are variables.
>
> I can't seem to get it to work. If I have the following format:
>Format(Number, "###0.0") and numbers
> .1, 100.1 and 10


You format the number first
I dislike the VB Format Function, but it does work.

In a textbox you simply use a Right Justified Textbox
- note it must be multi-line for RHJ to work

If you want to Print a number to an object X then :

CurrentX = RightColX - X.TextWidth( MyFormattedNumber )
X.Print MyFormattedNumber;

In other words you decide on the rightmost bit and then move the print
head to the left.

HTH

Daryl Muellenberg

2004-05-27, 2:31 pm

For a text box on the form, just set the Alignment property of the textbox
to 1 - Right Justify.

Daryl

"Thomas Scheiderich" <tfs@deltanet.com> wrote in message
news:10bb9h6csak7m2c@corp.supernews.com...
> How do I right justify a number for displaying on a printout or in a
> textbox. These are not objects. They are variables.
>
> I can't seem to get it to work. If I have the following format:
> Format(Number, "###0.0") and numbers
> .1, 100.1 and 10
>
> I end up with
> 0.1
> 100.1
> 10.0
> But I want to end up with:
> 0.1
> 100.1
> 10.0
>
> How do I do this?
>
> Thanks,
>
> Tom
>
>
>



Thomas Scheiderich

2004-05-28, 2:30 am


"Daryl Muellenberg" <dmuellenberg@agris.com> wrote in message
news:O#6x6LBREHA.1312@TK2MSFTNGP12.phx.gbl...
> For a text box on the form, just set the Alignment property of the textbox
> to 1 - Right Justify.


This only works if you want to right justify the whole control.

In my case, I wanted to use the text box to display data from my database.
In one line, I wanted to have about 10 variables and right justify them all.

The format(format(num,"xxx.0.00"),"@@@@@") did what I was looking for. Not
sure why it right justifies, however.

Thanks,

Tom
>
> Daryl
>
> "Thomas Scheiderich" <tfs@deltanet.com> wrote in message
> news:10bb9h6csak7m2c@corp.supernews.com...
a[color=darkred]
>
>



Thomas Scheiderich

2004-05-28, 2:30 am

"Rick Rothstein" <rickNOSPAMnews@NOSPAMcomcast.net> wrote in message
news:#0Anjm8QEHA.736@TK2MSFTNGP10.phx.gbl...
> or in a
> format:
>
> Or....
>
> strFormat = "0.0" ' you don't need the all those # signs
> n = 6 ' size of field to right justify in
> strOutput = Format$(Format$(number, strFormat), String$(n, "@"))
>
> The last line is the generic form of this (for n=6):
>
> strOutput = Format$(Format$(number, strFormat), "@@@@@@")
>
> where the inner Format statement formats the number to one decimal place
> and the outer formats that to a right-justified string.


This seemed to have solved my problem. It was exactly what I was looking
for.

But why does the outside format right justify the string?

Thanks,

Tom
>
> Rick - MVP
>



Rick Rothstein

2004-05-28, 5:30 am

> > Or....
place[color=darkred]
>
> This seemed to have solved my problem. It was exactly what I was

looking
> for.
>
> But why does the outside format right justify the string?


That's what the @ symbols do... the number of @ symbols is how many
characters wide the field will be and the @ symbols says fill the field
in from the right and when you run out of characters, uses spaces for
the remainder. If you put an exclamation point (!) in front of the
string, it will be left-justified within the field width specified.
Believe it or not, this is all in the help files, but Microsoft buried
it so deep, it is hard to find. Call up the help files, go to the Index
tab, type in "format function" (but don't use the quote marks), click on
the "See Also" link at the top of the page, and select "User-Defined
String Formats" from the list box that appears. Told you it was hard to
find.<g> By the way, you might want to look at the other "user-defined"
formats in the list.

Rick - MVP

Thomas Scheiderich

2004-05-28, 5:30 am

"Rick Rothstein" <rickNOSPAMnews@NOSPAMcomcast.net> wrote in message
news:ue$JM4IREHA.3300@TK2MSFTNGP09.phx.gbl...
> place
> looking
>
> That's what the @ symbols do... the number of @ symbols is how many
> characters wide the field will be and the @ symbols says fill the field
> in from the right and when you run out of characters, uses spaces for
> the remainder. If you put an exclamation point (!) in front of the
> string, it will be left-justified within the field width specified.
> Believe it or not, this is all in the help files, but Microsoft buried
> it so deep, it is hard to find. Call up the help files, go to the Index
> tab, type in "format function" (but don't use the quote marks), click on
> the "See Also" link at the top of the page, and select "User-Defined
> String Formats" from the list box that appears. Told you it was hard to
> find.<g> By the way, you might want to look at the other "user-defined"
> formats in the list.


That explains it.

Thanks,

Tom
>
> Rick - MVP
>



Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com