Home All Groups Group Topic Archive Search About
Author
12 Oct 2005 3:53 PM
Carlo
Hi
i need to write double number to a fixed lengh, es.:

3    ->  3.00
3.3 -> 3.30

i use format
Format(Text, "00.00")
but it isnt ezactly what i need, this returns:
text=3.3    format-> 03.30 i need <blank>3.30
do you know what have i to write???
i tried Format(Text, "##.0") but it doesnt work
thanks
carlo



thanks
carlo

Author
12 Oct 2005 4:13 PM
Ken Halter
"Carlo" <carletto.m@NOSPAMgmail.com> wrote in message
news:%23i6onU0zFHA.3856@tk2msftngp13.phx.gbl...
> Hi
> i need to write double number to a fixed lengh, es.:
>
> 3    ->  3.00
> 3.3 -> 3.30
>
> i use format
> Format(Text, "00.00")

Use....

Format$(YourNumber, "0.00")

That ensures a leading 0 and 2 decimal places.

3 will be 3.00

point 3 will be 0.30

9993.3 will be 9993.30

etc.




--
Ken Halter - MS-MVP-VB - http://www.vbsight.com
DLL Hell problems? Try ComGuard - http://www.vbsight.com/ComGuard.htm
Please keep all discussions in the groups..
Author
12 Oct 2005 4:24 PM
Carlo
thanks a lot
but this isnt what i need

i solve the problem in this way:
        sRiga = sRiga & String(7 - Len(Format(.Text, "0.00")), " ") &
Format(.Text, "0.00") & vbTab

thanks
carlo


Show quoteHide quote
"Ken Halter" <Ken_Halter@Use_Sparingly_Hotmail.com> ha scritto nel messaggio
news:OZn1uf0zFHA.4032@TK2MSFTNGP15.phx.gbl...
> "Carlo" <carletto.m@NOSPAMgmail.com> wrote in message
> news:%23i6onU0zFHA.3856@tk2msftngp13.phx.gbl...
>> Hi
>> i need to write double number to a fixed lengh, es.:
>>
>> 3    ->  3.00
>> 3.3 -> 3.30
>>
>> i use format
>> Format(Text, "00.00")
>
> Use....
>
> Format$(YourNumber, "0.00")
>
> That ensures a leading 0 and 2 decimal places.
>
> 3 will be 3.00
>
> point 3 will be 0.30
>
> 9993.3 will be 9993.30
>
> etc.
>
>
>
>
> --
> Ken Halter - MS-MVP-VB - http://www.vbsight.com
> DLL Hell problems? Try ComGuard - http://www.vbsight.com/ComGuard.htm
> Please keep all discussions in the groups..
>
Author
12 Oct 2005 4:35 PM
Bob Butler
"Carlo" <carletto.m@NOSPAMgmail.com> wrote in message
news:etEJ%23l0zFHA.736@tk2msftngp13.phx.gbl
> thanks a lot
> but this isnt what i need
>
> i solve the problem in this way:
>         sRiga = sRiga & String(7 - Len(Format(.Text, "0.00")), " ") &
> Format(.Text, "0.00") & vbTab

You are trying to right-justify the value in a 7-character field?  Maybe
something like

sRiga=Format$("###0.00",CSng(.Text)) & vbTab

or

sRiga=Right$(Space$(4) & Format$("0.00",CSng(.Text)),7) & vbTab

Both avoid the implicit string->number conversion and doing the same format
call twice.

--
Reply to the group so all can participate
VB.Net: "Fool me once..."
Author
12 Oct 2005 5:27 PM
Rick Rothstein [MVP - Visual Basic]
> > thanks a lot
> > but this isnt what i need
> >
> > i solve the problem in this way:
> >         sRiga = sRiga & String(7 - Len(Format(.Text, "0.00")),
" ") &
> > Format(.Text, "0.00") & vbTab
>
> You are trying to right-justify the value in a 7-character
field?  Maybe
> something like
>
> sRiga=Format$("###0.00",CSng(.Text)) & vbTab
>
> or
>
> sRiga=Right$(Space$(4) & Format$("0.00",CSng(.Text)),7) & vbTab
>
> Both avoid the implicit string->number conversion and doing the
same format
> call twice.

Hmm! Funny syntax on your Format statements there Bob... did you
forget to drink your coffee this morning?<g>

Also, that first statement won't work (even after reversing the
arguments in the Format statement); multiple # signs do not hold
multiple places. Since you have a zero in front of the decimal
point, the three # signs you show will not have any effect (leave
them off and you will get the same output). The second statement
you show will work fine though (after reversing the Format
function's arguments of course), although you omitted the
concatenation of sRiga onto the front of the statement. Lack of
coffee, right?

Another coding possibility is the following

sRiga = sRiga & Format$(Format$(3.3, "0.00"), "@@@@@@@") & vbTab

True, this uses the Format function twice, but each usage is
different.

Rick
Author
12 Oct 2005 6:04 PM
Bob Butler
"Rick Rothstein [MVP - Visual Basic]"
<rickNOSPAMnews@NOSPAMcomcast.net> wrote in message
news:eYYX8I1zFHA.3336@TK2MSFTNGP12.phx.gbl
<cut>
> Hmm! Funny syntax on your Format statements there Bob... did you
> forget to drink your coffee this morning?<g>

LOL
I don't drink coffee; perhaps I should start!

> Also, that first statement won't work (even after reversing the
> arguments in the Format statement); multiple # signs do not hold
> multiple places. Since you have a zero in front of the decimal
> point, the three # signs you show will not have any effect (leave
> them off and you will get the same output).

I thought I remembered the # as being leading 0 or space...  I don't
actually use the Format function very much so these little details tend to
escape the brain over time.

> The second statement
> you show will work fine though (after reversing the Format
> function's arguments of course), although you omitted the
> concatenation of sRiga onto the front of the statement. Lack of
> coffee, right?

No, just didn't read the OP carefully enough

> Another coding possibility is the following
>
> sRiga = sRiga & Format$(Format$(3.3, "0.00"), "@@@@@@@") & vbTab
>
> True, this uses the Format function twice, but each usage is
> different.

That doesn't bug me nearly as much as the code that did exactly the same
formatting twice.  I still prefer not using Format if I can do it another
way just as easily.  Not sure why I dislike it like I do but I do try to
avoid it.

--
Reply to the group so all can participate
VB.Net: "Fool me once..."
Author
12 Oct 2005 6:21 PM
Duane Bozarth
....
> > You are trying to right-justify the value in a 7-character
> field?  Maybe
> > something like

[sic, modified to remove syntax error and leave only the format]

> > Format$(CSng(.Text), "###0.00")
....

> Also, that ... statement won't work (even after reversing the
> arguments in the Format statement); multiple # signs do not hold
> multiple places. ...

Which is why I've always thought Format$() either needed rules similar
to Fortran FORMAT statements or another placekeeper for precisely the
purpose Bob wanted # to serve.  Actually, I never understood why #
wasn't defined in that manner...
Author
12 Oct 2005 4:17 PM
Rick Rothstein [MVP - Visual Basic]
> i need to write double number to a fixed lengh, es.:
>
> 3    ->  3.00
> 3.3 -> 3.30
>
> i use format
> Format(Text, "00.00")
> but it isnt ezactly what i need, this returns:
> text=3.3    format-> 03.30 i need <blank>3.30
> do you know what have i to write???
> i tried Format(Text, "##.0") but it doesnt work

The 0 and # characters are place holders for numbers... the 0
forces a zero to be outputted if there is no digit in the
specified place and the # outputs the empty string ("") if no
digit exists. Now, on to your question. I'm afraid it isn't
entirely clear what you want in the general situation. For
example, can you have a number whose integer part is 2 or more
digits (such as 12.34)? If so, what did you want to output for
this situation? In other words, are you always looking to output a
leading blank space? Or are your numbers confined to a maximum of
two digits in the integer part and you want the blank space only
when a single digit appears in the integer portion of the number?
Or something else I haven't thought of? Also, what did you want
outputted if the number has no integer portion (such as .456)... a
leading blank followed by a zero or just two leading blanks?

Rick