borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2007 Andrea Tincaniborder

AndreaVB | Forum | News | Downloads | Register | Help | Member List | Statistics | Search | PM | Profile

Print This Topic
Previous Topic (API Text on forms?)Next Topic (Is there any MAX function in VB6 ?) New Topic New Poll Post Reply
AndreaVB Forum : VB General : How to Round a number ? Solved Topic
Poster Message
iliekater
Level: Master

Registered: 04-02-2005
Posts: 123

icon How to Round a number ?

Here's a tricky one : If someone wants to to get the rounded value of a number , he can use the Round function , for example :
MyRoundedValue = Round(MyNumber , 2)

my problem , however , is that I don't want to round my value to a specific decimal place , instead I want to round itto a specific decimal value , that is 0.75 .
for Example I want 3.79 to be rounded to 4 , while 2.68 to the value 2 .
Is it possibleto use the Round function (or any other) for this purpose ?
Up to now , the only way I have thought about , is comparing the double value with integer part (using the Int function) , but this requires too much of code ...

22-03-2007 at 10:30 PM
View Profile Send Email to User Show All Posts | Quote Reply
admin
Level: Administrator


Registered: 04-04-2002
Posts: 530
icon Re: How to Round a number ?

you can use this trick:


MyRoundedValue = Round(MyNumber - 0.25)


so 3.79 is rounded to 4 but 3.74 is rounded to 3

    

____________________________
AndreaVB

23-03-2007 at 08:14 AM
View Profile Send Email to User Show All Posts Visit Homepage | Quote Reply
JLRodgers
Level: Moderator

Registered: 04-04-2002
Posts: 1616
icon Re: How to Round a number ?

There are of course more complex ways like:

rounded_number = Int(x) + IIf(x - Int(x) - 0.75 >= 0, 1, 0)

but just subtracting the .25 from the number and using the round function is easier (and probably a few milliseconds quicker).

____________________________
Everywhere's Local (classifieds, job postings, & more for everycity in the world - user entered)

23-03-2007 at 06:57 PM
View Profile Send Email to User Show All Posts Visit Homepage | Quote Reply
iliekater
Level: Master

Registered: 04-02-2005
Posts: 123
icon Re: How to Round a number ?

Oh man , you are a genius !

23-03-2007 at 10:38 PM
View Profile Send Email to User Show All Posts | Quote Reply
AndreaVB Forum : VB General : How to Round a number ? Solved Topic
Previous Topic (API Text on forms?)Next Topic (Is there any MAX function in VB6 ?) New Topic New Poll Post Reply
Surf To:


Not Logged In? Username: Password: Lost your password?
Partners: Download Actual Software | Free Software Download
borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2007 Andrea Tincaniborder