New topic: How to round a decimal to nearest x?
<http://forums.realsoftware.com/viewtopic.php?t=39779> Page 1 of 1 [ 3 posts ] Previous topic | Next topic Author Message tseyfarth Post subject: How to round a decimal to nearest x?Posted: Sun Jul 10, 2011 6:50 pm Joined: Sat Dec 04, 2010 9:14 pm Posts: 288 Hello, How do I round a decimal of say .256 to .25 or up to .26? Thanks Tim Top charonn0 Post subject: Re: How to round a decimal to nearest x?Posted: Sun Jul 10, 2011 7:31 pm Joined: Mon Apr 02, 2007 2:08 am Posts: 480 Location: San Francisco, CA, USA The built-in rounding functions only return integers. In order to round a decimal you need to multiply and divide by powers of 10: Code: Dim x As Double = 1.2345 x = Round(x * 10) / 10 //Round to 1 decimal place x = Round(x * 100) / 100 //Round to 2 decimal places x = Round(x * 1000) / 1000 //Round to 3 decimal places x = Round(x * 10000) / 10000 //Round to 4 decimal places Note that the Round function will round up or round down according to the rules of rounding. To always round up use Ceil and to always round down use Floor. _________________ Boredom Software Top tseyfarth Post subject: Re: How to round a decimal to nearest x?Posted: Sun Jul 10, 2011 10:00 pm Joined: Sat Dec 04, 2010 9:14 pm Posts: 288 Thanks charonn0, that works perfectly. Tim Top Display posts from previous: All posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost timeSubject AscendingDescending Page 1 of 1 [ 3 posts ] -- Over 1500 classes with 29000 functions in one REALbasic plug-in collection. The Monkeybread Software Realbasic Plugin v9.3. http://www.monkeybreadsoftware.de/realbasic/plugins.shtml [email protected]
