Need help with rounding numbers.
I understand the concept of rounding I guess, all I know is math.round(), but I need a break down of it so I can round a number like this: 14.789543 to this: 14.8 because after the 7 there is an 8, which means you would round the seven up. Can anyone tell me how this is done?
And some more detail, I need to round to the nearest tenth.
Re: Need help with rounding numbers.
There is second optional parameter that can be sent to this function that returns it to a decimal point. So...
math.round(14.789543, 1) would return 14.8.
Re: Need help with rounding numbers.
Quote:
Originally Posted by
sra3cf
There is second optional parameter that can be sent to this function that returns it to a decimal point. So...
math.round(14.789543, 1) would return 14.8.
Thaty works somewhat, the only problem is that It's not rounding the decimal, it's rounding to the nearest whole digit.
I need it to round to the nearest tenth.
Re: Need help with rounding numbers.
Well are you storing the result as an Integer? Integers cannot hold Decimal places.
This is wrong:
vb.net Code:
Dim x As Integer = Math.Round(14.7859, 1)
'This will remove the decimal!!!
This is more logical:
vb.net Code:
Dim x As Double = Math.Round(14.7859, 1)
'This will not!
Doubles are designed to perform math operations, and the Math.Round() Function returns a double.
Re: Need help with rounding numbers.
Decimal will work better than Double too...
-tg
Re: Need help with rounding numbers.
Alright, I figured out my problem, my problem was that I was rounding a number from a textbox, so I was using CInt when I should have been using CDbl. Now can anyone tell me how to round to the nearest hundredth?
Re: Need help with rounding numbers.
Someone already did, have you tried changing 2nd parameter of math.round at all?
Re: Need help with rounding numbers.
Quote:
Originally Posted by
techgnome
Decimal will work better than Double too...
-tg
Forgot about Decimal. I just usually use Double because I've never had a need to use it yet..
Re: Need help with rounding numbers.
Before I can mark this resolved, how could I round to say, the nearest 10, like if the number is 56, it will round up to 60.
Re: Need help with rounding numbers.
divide by 10, round, multiply by 10