02-25-2019 02:49 AM
Hi there!
I want to truncate the values from the second decimal onwards. I believe it can be done with fixed-point conversion, but I don't know how to configure it (I attached my fxp config). With the actual configuration, if I enter a value of 0.24147, the program truncates it to 0.25, and I want it to truncate it to 0.24. Any suggestions?
Thank you in advance.
Best regards,
Iñigo.
02-25-2019 03:13 AM
Do you really want to truncate, or just display with less precision?
If only display, then change the properties of the indicator to display only 2 decimal places (why give up precision that might help in later calculations).
If you do really need to truncate, you can multiply by 10^decimal places. then round to nearest, then divide by 10^decimal places.
0xDEAD
02-25-2019 03:16 AM
02-25-2019 03:25 AM - edited 02-25-2019 03:29 AM
Hello,
Thank you for your fast replies. I've achieved this solution, I don't know if is the same as yours, but it works for me, it only truncates the values without rounding.
02-25-2019 03:28 AM
Fixed point has nothing to do with anything "decimal". It is "fixed point" in binary. Many decimal fractions cannot be represented in binary and vice versa.
Are you just looking for a formatting option? If it is always two decimal digits and you typically don't do division and fancy math, maybe you want to carry everything as 100x larger integers (e.g. cents instead of dollars) and just convert for display. Please explain what you are trying to achieve. To approximately truncate down to 2 decimal digits, multiply by 100, round to -inf, and divide by 100 again. Again, the limitations of floating point remains.
02-25-2019 03:31 AM
02-25-2019 03:35 AM - edited 02-25-2019 04:47 AM
@IñigoP wrote:
Thank you for your fast replies. I've achieved this solution, I don't know if is the same as yours, but it works for me, it only truncates the values without rounding.
You have not specified the treatment of negative inputs, but from your code it seems you want to truncate to two decimal digits towards zero. Here's what I would do. No comparison operation needed. 😉
You can even combine the last two functions into a compound node if you want (don't forget to invert):
02-25-2019 03:37 AM
Yes, the thing is if I have 0.242 I want it to round down to 0.24, but when I have a -0.242 rounding up gives -0.24.
Thank you for cleaning my code by the way.