Re: atom_to_float32 precision
- Posted by CChris <christian.cuvier at agr?culture?gouv.fr> Oct 25, 2007
- 573 views
Jonas Temple wrote: > > Is there any way to control the precision of the output of atom_to_float32? > I want to take a number that might have the value: > > 123.450000 > > And ensure it comes out as: > > 123.45 > > Jonas Temple > <a href="http://www.innovativesys.net">http://www.innovativesys.net</a>
function round_with granularity(atom a,atom granularity) -- rounds a to nearest multiple of granularity -- also check out w32round() in w32utils, distributed with win32lib 0.70.x atom rem if granularity<0.0 then granularity = -granularity elsif granularity = 0.0 then puts(2,"Rounding to a multiple of 0!") return a end if rem=remainder(a,granularity) if rem+rem>=granularity then a += granularity end if return a-rem end function
Also, if you need fixed decimal arithmetic, converting everything to integers is the way, as Rob and a few others have underlined. Also, modifiying this slightly will give you other rounding mode, according to your exact needs. CChris