|
-
Apr 23rd, 2001, 10:47 PM
#1
Thread Starter
Frenzied Member
hi, how do I always round a number up a number to the whole digit?
thanks
dimava
NXSupport - Your one-stop source for computer help
-
Apr 24th, 2001, 02:25 AM
#2
Frenzied Member
Add .999?
This should almost always work.
Add .99999999999 to the number and then convert to integer. I assume you can figure out the code to do it.
Live long & prosper.
The Dinosaur from prehistoric era prior to computers.
Eschew obfuscation!
If a billion people believe a foolish idea, it is still a foolish idea!
VB.net 2010 Express
64Bit & 32Bit Windows 7 & Windows XP. I run 4 operating systems on a single PC.
-
Apr 24th, 2001, 04:13 AM
#3
Addicted Member
You need to add.4999999999 instead of .9999999999.
CInt(4.9 + .999999) returns 6.
-
Apr 24th, 2001, 05:12 AM
#4
Thread Starter
Frenzied Member
NXSupport - Your one-stop source for computer help
-
Apr 24th, 2001, 10:29 AM
#5
Frenzied Member
Strange roundoff error.
Strange that 4.9 + .9999999 equals 6. Are you sure? Wouldn't you get 5.9 if you added one? How could adding .999999 result in more than adding one?
I thought you wanted the next higher integer no matter what. If so, perhaps truncate to integer and then add one.
Adding .499999 does not seem correct. Why not .5?
Live long & prosper.
The Dinosaur from prehistoric era prior to computers.
Eschew obfuscation!
If a billion people believe a foolish idea, it is still a foolish idea!
VB.net 2010 Express
64Bit & 32Bit Windows 7 & Windows XP. I run 4 operating systems on a single PC.
-
Apr 24th, 2001, 11:36 AM
#6
transcendental analytic
Cint doesnt truncate to integer, it rounds to closest integer
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Apr 24th, 2001, 12:11 PM
#7
Frenzied Member
In that case it depends on which function you use. If I remember right, Int() just truncates the number.
Harry.
"From one thing, know ten thousand things."
-
Apr 24th, 2001, 12:36 PM
#8
transcendental analytic
yeah but i think you should round with cint this time:
Cint(number+0.5)
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
May 3rd, 2001, 04:37 AM
#9
Registered User
You guys have forgotten that if int(number) = number then you shouldn't add anything to the number, however, if int(number) <> number then you should add an integer. That's how to accurately round up.
Don't forget to allow for negative numbers though.
-
May 3rd, 2001, 04:05 PM
#10
Frenzied Member
Well not really, if you notice Guv suggested adding 0.999999 or thereabouts. If you add this to an integer and truncate it, you get the original integer.
Harry.
"From one thing, know ten thousand things."
-
May 3rd, 2001, 05:54 PM
#11
Registered User
Harry, what about if I have 1.0000001?
1.00000001
0.99999990+
---------------
1.99999991 which will be rounded down to 1.
On the other hand, (int(1.0000001) = 1.0000001) will return false so you can catch the error and round it to 2 rather than 1.
Comparing int(number) vs number is the most accurate way I can think of to do this...
-
May 3rd, 2001, 06:35 PM
#12
Frenzied Member
Well there is a limit to how accurately a floating point number can be represented in binary, so it is certainly possible to add the closest value to 1 that is less than 1. At greater accuracy than that, it's pointless trying to be accurate because you are limited by the machine.
Harry.
"From one thing, know ten thousand things."
-
May 4th, 2001, 08:47 AM
#13
transcendental analytic
Sometimes it seems like the last digit is dropped off when displayed, but i guess the displayed value is not equal to the stored, if you pass 0.999999999 there might just be a fraction that is smaller than 0.000000001 thats why i suggested cint(x+0.5)
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
May 6th, 2001, 07:40 AM
#14
Registered User
Kedaman, I don't think your appraoch is necessarily appropriate. Consider if I want to round the two numbers 0 and 1 to nearest integer that is they should return 0 and 1:
debug.print CInt(0 + 0.5)
debug.print CInt(1 + 0.5)
This is because CInt and CLng always round it to the nearest even number.
Better is to compare int(number) vs number.
-
May 6th, 2001, 08:21 AM
#15
transcendental analytic
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|