New topic: round single to 2 decimal place
<http://forums.realsoftware.com/viewtopic.php?t=41315> Page 1 of 1 [ 7 posts ] Previous topic | Next topic Author Message bigbear Post subject: round single to 2 decimal placePosted: Fri Nov 04, 2011 12:52 pm Joined: Wed Mar 15, 2006 7:32 pm Posts: 218 How can i make a single round its value to 2 decimal places? for example: Code:dim s as single s=87.326736489 I want s to hold the value 87.33 instead. _________________ RealBasic 2007 Rev.1 on MacBook Pro 10.6.1 and RealBasic 2007 Rev.1 on Win XP Top wbgookin Post subject: Re: round single to 2 decimal placePosted: Fri Nov 04, 2011 1:25 pm Joined: Sat Aug 22, 2009 9:44 am Posts: 257 It's probably not terribly efficient speed-wise, but how about: Code:s = val(format(s, "#.##")) Bill Top charonn0 Post subject: Re: round single to 2 decimal placePosted: Fri Nov 04, 2011 1:49 pm Joined: Mon Apr 02, 2007 2:08 am Posts: 569 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 Single= 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 timhare Post subject: Re: round single to 2 decimal placePosted: Fri Nov 04, 2011 1:52 pm Joined: Fri Jan 06, 2006 3:21 pm Posts: 10382 Location: Portland, OR USA Code:dim s as single dim n as integer s=87.326736489 n = round(s * 100) s = n/100 But note that s may still be just a close approximation of the 2 decimal result, ie., 87.330000001 or similar. Top Jason_Adams Post subject: Re: round single to 2 decimal placePosted: Fri Nov 04, 2011 1:52 pm Joined: Fri Nov 10, 2006 4:10 pm Posts: 1015 Location: Michigan, USA The format method is fine unless you're needing to save on speed, in which case the rounding method keeps one from recursive conversion. But, a quick amendment, be sure to add the hyphen to your format, or you'll lose your sign: Code:Dim s As Single = 52.23453 s = Format(s, "-#.##").Val Hope this helps. _________________ Windows 7 Ultimate x64 Windows XP Pro SP3 Ubuntu 11.04 via Virtual Box RS Enterprise 2011r3 "Christianity has not been tried and found wanting; it has been found difficult and not tried." - G.K. Chesterton Top silverpie Post subject: Re: round single to 2 decimal placePosted: Fri Nov 04, 2011 1:58 pm Joined: Sat Oct 01, 2005 9:55 am Posts: 478 CDbl, not Val. Format produces results in the user's selected format (which may or may not have a period for the decimal sign). At least in theory, CDbl will expect the same, while Val always expects US format. (I say in theory, because there are some odd corner cases like French/Switzerland.) Top Keith DeLong Post subject: Re: round single to 2 decimal placePosted: Fri Nov 04, 2011 3:14 pm Joined: Fri Sep 30, 2005 11:13 am Posts: 41 Danger Will Robinson! s = val(format(s, "#.##")) truncates to 2 decimal places, it does not round! _________________ Keith DeLong Redcort Software Publishers of Virtual TimeClock® and Virtual TimeClock Pro® Time clock software to easily track and print employee time cards. Top Display posts from previous: All posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost timeSubject AscendingDescending Page 1 of 1 [ 7 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]
