Round a float
dear Reader,
could you tell me please, how to round a float which has 5 numbers after the dot (5 decimals?) to a number that has only 2 decimals?
like
0.12345465
to
0.12
Any help appreciated!
thanks
tEd
Julien,
I miss my math teacher from ground school:)
thank you!
tEd
round up
On 24 Feb 2008, at 17:22, Rabin Julien wrote:
> Something like this ?
Actually, if you do want rounding, rather than truncation, it's a one-
liner:
[sprintf %.2f]
Nick Rothwell / Cassiel.com Limited
www.cassiel.com
www.myspace.com/cassieldotcom
www.last.fm/music/cassiel
www.reverbnation.com/cassiel
www.linkedin.com/in/cassiel
www.loadbang.net
Yep, it's way more fun to do things by hand :) Should have read on...
My colleague suggested that the sprintf way may not be the fastest after all, even though it looks way more compact. So a made a little speedtest featuring sprintf, expr and standard max objects.
The speed says it all - oh and another thing: how do you change the number of decimals when using [sprintf %.#f] ? I can think of so many situations where i will need this :)
On 25 Feb 2008, at 11:11, Bas van der Graaff wrote:
> My colleague suggested that the sprintf way may not be the fastest
> after all, even though it looks way more compact.
Sure - the Max tokeniser is called to turn the intermediate string
back into a float, so it will be slower than the alternatives, should
that matter.
> oh and another thing: how do you change the number of decimals when
> using [sprintf %.#f] ?
I don't think I received that memo.
[sprintf %.*f] should do it according to the specs I'm looking at, but
the Max sprintf object doesn't seem to support that.
-- N.
Nick Rothwell / Cassiel.com Limited
www.cassiel.com
www.myspace.com/cassieldotcom
www.last.fm/music/cassiel
www.reverbnation.com/cassiel
www.linkedin.com/in/cassiel
www.loadbang.net
Quote: tedor wrote on Sun, 24 February 2008 08:28
----------------------------------------------------
> dear Reader,
>
> could you tell me please, how to round a float which has 5 numbers after the dot (5 decimals?) to a number that has only 2 decimals?
>
> like
>
> 0.12345465
>
> to
>
> 0.12
>
> Any help appreciated!
>
> thanks
>
> tEd
----------------------------------------------------
Also, lp.round here:
I mean, round.
mz
> [sprintf %.*f] should do it according to the specs I'm looking at, but
> the Max sprintf object doesn't seem to support that.
sprintf %.3f will round to 3 places, %.4f to four, etc.
But I noticed the following (maybe someone has an explanation to this behavior):
if I feed a sprintf %.3f the number 0.2345, it outputs 0.234 (rounds down)
sending 0.23451 will cause an output of 0.235 (rounds up)
hmmm...
best,
Zachary
Quote: Zachary Seldess wrote on Wed, 27 February 2008 02:45
----------------------------------------------------
> if I feed a sprintf %.3f the number 0.2345, it outputs 0.234 (rounds down)
>
----------------------------------------------------
Because you can't represent 0.2345 *exactly* in binary.
The closest binary representation is 0.23449999
So guess what happens when you round?
Go to for further details.
If I had a dollar for every time I've had to explain this...
Quote: Peter Castine wrote on Wed, 27 February 2008 17:37
----------------------------------------------------
> If I had a dollar for every time I've had to explain this...
----------------------------------------------------
That int would probably have wrapped around by now and you'd actually owe money :)
Bas van der Graaff schrieb:
> Quote: Peter Castine wrote on Wed, 27 February 2008 17:37
> ----------------------------------------------------
>> If I had a dollar for every time I've had to explain this...
> ----------------------------------------------------
>
> That int would probably have wrapped around by now and you'd actually owe money :)
Ha, the times I have laughed that much hasn't wrapped yet... ;-))))
Stefan
--
Stefan Tiedje------------x-------
--_____-----------|--------------
--(_|_ ----|-----|-----()-------
-- _|_)----|-----()--------------
----------()--------www.ccmix.com
Quote: Bas van der Graaff wrote on Wed, 27 February 2008 18:03
----------------------------------------------------
> Quote: Peter Castine wrote on Wed, 27 February 2008 17:37
> ----------------------------------------------------
> > If I had a dollar for every time I've had to explain this...
> ----------------------------------------------------
>
> That int would probably have wrapped around by now and you'd actually owe money :)
----------------------------------------------------
Oh, with 64-bit unsigned integers, I think I can handle it.-