Link to home
Start Free TrialLog in
Avatar of bemson
bemson

asked on

Using Format in loops

I have a loop

for a=1 to 5

printer.print format(firstvalue(a),"####.##"), format(secondvalue(a),"###.00"), format(thirdvalue(a)," ###.##")

next a

How do I get the following layout using format?

   7.89      0.34    6.12
1245.00   3452.56   34.00
    .01     70.00    1.34

etc

also variation for no decimal places in say firstvalue?

Thank you
Avatar of Éric Moreau
Éric Moreau
Flag of Canada image

Use this mask: "####.00"
Avatar of bemson
bemson

ASKER

format shown after submission not the same as I typed it

using your suggestion for following values

78,2456,0.12
23.34,0.12,346

I get the following
- represents a space in case of what I type is not clear

78.00,2456.00,.12
23.34,.12----,346.00

- represents a space in case of what I type is not clear

I want the following


78.00,2456.00,----.12
23.34,----.12, 346.00

thank you

You can use something like this:

Private Sub Command1_Click()
Debug.Print EMFormat(7.89, 7) & "," & EMFormat(0.34, 7) & "," & EMFormat(6.12, 7)
End Sub

Private Function EMFormat(ByVal psngValue As Single, ByVal pbytLen As Byte) As String
    EMFormat = Right$(Space$(pbytLen) & Format$(psngValue, "####.00"), pbytLen)
End Function
Avatar of bemson

ASKER

OK,

However I would like to vary the function capabilty
by changing 7 to 4 to get the following
for the first two values, ie 78 first line, 23 second line

Printer.Print EMFormat(78, 4), EMFormat(2456, 7), EMFormat(0.12, 7)
Printer.Print EMFormat(23.34, 4), EMFormat(0.12, 7), EMFormat(346, 7)

although 23 should be 24 if figure was 23.78

78,2456.00,---.12
23,----.12,346.00

what I get is:

8.00,2456.00,---.12
3.34,----.12,346.00

thank you
ASKER CERTIFIED SOLUTION
Avatar of Éric Moreau
Éric Moreau
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Why a B when I give you exactly what you want?
Avatar of bemson

ASKER



I now have to figure out how to use your function to perform a function that vb should have for all other combinations with both format and number of characters,
in any case by using your function, if Format2 and using "###0" then default obviously would be 4, so why specify pbytLen?

if I pass 346.3,2 to your function I get 46, no error that it should be 346

in dos with qbasic
the statement "print using "####", firstvalue
provided the spacing

I would have got " 346"



obviously whoever designed vb has missed the point of formatting

basically I (and others) need a single function to provide spaces to properly format numbers

So for every format I need I have to create a new function for every possibility, both for number formatting & how many numbers to display

If you have a better solution I am willing to give 100
points and an A

even so, thanks for your help