Connecting Tech Pros Worldwide Help | Site Map

Get highest value from Math.Round

Newbie
 
Join Date: Aug 2006
Posts: 10
#1: 3 Weeks Ago
Hi..
I have tried following coding to get highest value.
EX: If I type 4.341111111111, It should get the value 4.35.
If I type 9.132222, It should be 9.14 not 9.13.

Expand|Select|Wrap|Line Numbers
  1. MsgBox(Math.Round(Val(TextBox1.Text), 2))
  2.  
  3. ' This is retrieving min value. Please help me to solve  this issue..
  4. All Codings are with Vb 2008
  5.  

Thank You,
Regards,
Supun Silva
best answer - posted by Plater
Math.Ceiling() always rounds up.
14.9 or 14.1 will always be 15.
It might work in decimal form as well with an overload
OR combine with the multiply by 10 or 100 (experimenting is good)
tlhintoq's Avatar
Moderator
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 1,743
#2: 3 Weeks Ago

re: Get highest value from Math.Round


"rounding" is always done as 5 and higher round up, 4 and lower round down.
So the round function you are using is working as it was meant to.

You will probably have to write your own function for this uncommon need.
Newbie
 
Join Date: Aug 2006
Posts: 10
#3: 3 Weeks Ago

re: Get highest value from Math.Round


Do you have any idea for this.. Please help me??

Thank You,
Supun Silva
tlhintoq's Avatar
Moderator
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 1,743
#4: 3 Weeks Ago

re: Get highest value from Math.Round


Quote:
If I type 4.341111111111, It should get the value 4.35.
Multiply the float by 100 that will get you 434.1111111111
Make an int of that number getting you 434 since it will drop the decimal points
If the int is greater than the float then add 1 to the float getting you 435.1111111
Make an int of that getting you 435
Divide that by 100 getting you 4.35
Plater's Avatar
Moderator
 
Join Date: Apr 2007
Location: New England
Posts: 7,148
#5: 3 Weeks Ago

re: Get highest value from Math.Round


Math.Ceiling() always rounds up.
14.9 or 14.1 will always be 15.
It might work in decimal form as well with an overload
OR combine with the multiply by 10 or 100 (experimenting is good)
tlhintoq's Avatar
Moderator
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 1,743
#6: 3 Weeks Ago

re: Get highest value from Math.Round


Quote:

Originally Posted by Plater View Post

Math.Ceiling() always rounds up.
14.9 or 14.1 will always be 15.
It might work in decimal form as well with an overload
OR combine with the multiply by 10 or 100 (experimenting is good)

Way cool. I love learning new things. There are just so many pre-existing functions that it is impossible to know them all.

Funny that they didn't use a name that was a little more intuitive:
Round( )
RoundUp( )
RoundDown( )

This would have also caused all three methods to appear next to each other in the namespace and in Intellisearch, steering people toward them even if they didn't know about them.

Or an override
Round(float)
Round(float, Enum.UpDown)

This is an example of something I would add to my own namespace with the other method calls to make sense to me.

Expand|Select|Wrap|Line Numbers
  1. public static int RoundUp(float Value)
  2. {
  3.    return Math.Ceiling(Value);
  4. }
Newbie
 
Join Date: Aug 2006
Posts: 10
#7: 3 Weeks Ago

re: Get highest value from Math.Round


Hi.. Thanks for your reply. Please be kindly send me the RoundUp code for VB 2008. not for C++. Please send me full cording. I couldn't understand above coding..

Regards,
Supun Silva
tlhintoq's Avatar
Moderator
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 1,743
#8: 3 Weeks Ago

re: Get highest value from Math.Round


Quote:

Originally Posted by supun24 View Post

Hi.. Thanks for your reply. Please be kindly send me the RoundUp code for VB 2008. not for C++. Please send me full cording. I couldn't understand above coding..

Regards,
Supun Silva

Plater already told you how to do this:
Quote:

Originally Posted by Plater

Math.Ceiling() always rounds up.
14.9 or 14.1 will always be 15.
It might work in decimal form as well with an overload
OR combine with the multiply by 10 or 100 (experimenting is good)

Where you are currently using Math.Round just replace with Math.Ceiling
Newbie
 
Join Date: Aug 2006
Posts: 10
#9: 3 Weeks Ago

re: Get highest value from Math.Round


No.. it is receiving without decimal places. I need number with 2 decimal places.
Ex:
If I have 14.1423242424242 --- It should be 14.15.

Hope you understand.
Regards,
Supun Silva
tlhintoq's Avatar
Moderator
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 1,743
#10: 3 Weeks Ago

re: Get highest value from Math.Round


Hmmmm.... Could there be an answer somewhere in all of this if one were to combine several pieces of advice?

Multiply by 100
Math.Ceiling
Divide by 100
Reply