Limit number of decimal places in output from [flonum]
I have an [flonum] object and I have set the number of decimal places to be 2. Still the number that is sent to the outlet contains up to 13 decimals. Any suggestions on what I should do to avoid this?
The number of decimal places of the float is for display purposes only. This may help (note you may want to round the value…)
Thanks for the advise. I played a bit around with it and I often end up with several decimals in the lower float box as well. E.g 107.900002 both at the top and at the bottom.
This is another story…
107.90 doesn't exist as a 32bits float, it's either 107.900002 or 107.899986.
I invite you to search google or this forum with "IEEE 754".
Ok - thanks for the advise. However I still want to have a hard limit to 2 decimals. Maybe I can get around it by rounding it to the nearest valid float num or similar?
sprinf is your friend :)
[flonum] -> [sprintf %.2f] for two decimals.
Jan
sprintf will only guarantee a fixed number of decimal places in symbol mode.
Once you output a float, it's a float value (a float is a float is a float is a float).
Floats are binary values. Things like 0.02 are infinitely repeating fractions in binary. They cannot be represented exactly in binary, they can only be approximated. It's just like 1/3 in decimal--you can never write out the exact decimal representation. Trying to do so is hammering a round peg into a square hole, kids.
IEEE 754 is your friend. Srsly.
Hi Dad,
of course sprintf can only format as symbol/string. That it won't work as a float was already made very clear in this thread! But as KMLL didn't mention the context he needs that "hard" formatted value for (displaying could be one of the reasons among many others), why not pointing out a way?
Jan