Beginner VB

Moderators: None (Apply to moderate this forum)
Number of threads: 1244
Number of posts: 2990

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Help with formatting Posted by Loopy on 1 Apr 2002 at 3:47 PM
This message was edited by the Moderator at 2002-4-1 16:52:55

I'm having difficulty with the Format function. I have created some code to figure the averages of a set of inputs. When I type my code, I want the numbers to be in the format "0.00" but if the average ends up being a whole number (integer), the code will not force it to be followed by .00. In other words, if I want to get 480.00, I am only getting 480 with no decimal point or zeros. Here is my code, please help if you can. Thanks!



'create a loop to display the averages _
'of each score (verbal, quantitative, _
'and analytical) for all entered students

Dim a As Integer
For a = 1 To UBound(fStudents)
VAverage = (VAverage + fStudents(a).VScore / StudentIndex)
QAverage = (QAverage + fStudents(a).QScore / StudentIndex)
AAverage = (AAverage + fStudents(a).AScore / StudentIndex)
Next a

'display averages in the format "0.00"
'round averages up to the next 1/100th (.00)

VAverage = Format(Round(VAverage, 2), "0.00")
QAverage = Format(Round(QAverage, 2), "0.00")
AAverage = Format(Round(AAverage, 2), "0.00")



Report
Re: Help with formatting Posted by KDivad Leahcim on 2 Apr 2002 at 6:06 AM
: This message was edited by the Moderator at 2002-4-1 16:52:55

: I'm having difficulty with the Format function. I have created some code to figure the averages of a set of inputs. When I type my code, I want the numbers to be in the format "0.00" but if the average ends up being a whole number (integer), the code will not force it to be followed by .00. In other words, if I want to get 480.00, I am only getting 480 with no decimal point or zeros. Here is my code, please help if you can. Thanks!
:
:
:
: 'create a loop to display the averages _
: 'of each score (verbal, quantitative, _
: 'and analytical) for all entered students
:
: Dim a As Integer
: For a = 1 To UBound(fStudents)
: VAverage = (VAverage + fStudents(a).VScore / StudentIndex)
: QAverage = (QAverage + fStudents(a).QScore / StudentIndex)
: AAverage = (AAverage + fStudents(a).AScore / StudentIndex)
: Next a
:
: 'display averages in the format "0.00"
: 'round averages up to the next 1/100th (.00)
:
: VAverage = Format(Round(VAverage, 2), "0.00")
: QAverage = Format(Round(QAverage, 2), "0.00")
: AAverage = Format(Round(AAverage, 2), "0.00")
:

Format(16.725, "0.00") = 16.73
Format(16.724, "0.00") = 16.72
Format(16.7, "0.00") = 16.70
Format(16, "0.00") = 16.00

Seems that it works fine for me. Of course, I don't have the Round function so maybe that is messing it up somehow? Format appears to do it's own rounding though. Maybe that's only when it's a number, I don't know...
Report
Re: Help with formatting Posted by hobby on 2 Apr 2002 at 10:08 AM
: : This message was edited by the Moderator at 2002-4-1 16:52:55

: : I'm having difficulty with the Format function. I have created some code to figure the averages of a set of inputs. When I type my code, I want the numbers to be in the format "0.00" but if the average ends up being a whole number (integer), the code will not force it to be followed by .00. In other words, if I want to get 480.00, I am only getting 480 with no decimal point or zeros. Here is my code, please help if you can. Thanks!
: :
: :
: :
: : 'create a loop to display the averages _
: : 'of each score (verbal, quantitative, _
: : 'and analytical) for all entered students
: :
: : Dim a As Integer
: : For a = 1 To UBound(fStudents)
: : VAverage = (VAverage + fStudents(a).VScore / StudentIndex)
: : QAverage = (QAverage + fStudents(a).QScore / StudentIndex)
: : AAverage = (AAverage + fStudents(a).AScore / StudentIndex)
: : Next a
: :
: : 'display averages in the format "0.00"
: : 'round averages up to the next 1/100th (.00)
: :
: : VAverage = Format(Round(VAverage, 2), "0.00")
: : QAverage = Format(Round(QAverage, 2), "0.00")
: : AAverage = Format(Round(AAverage, 2), "0.00")
: :
:
: Format(16.725, "0.00") = 16.73
: Format(16.724, "0.00") = 16.72
: Format(16.7, "0.00") = 16.70
: Format(16, "0.00") = 16.00
:
: Seems that it works fine for me. Of course, I don't have the Round function so maybe that is messing it up somehow? Format appears to do it's own rounding though. Maybe that's only when it's a number, I don't know...
:

Try the FormatNumber function:

FormatNumber(16.725,2,vbtrue,vbfalse,vbfalse)




 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.