LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

truncate values

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.

Download All
0 Kudos
Message 1 of 8
(3,897 Views)

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

 

truncate.png

Message 2 of 8
(3,885 Views)

Hi Inigo,

 

why do you think you need FXP data here?

Why not just round your values as needed?

 

Two options:

check.png

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
Message 3 of 8
(3,883 Views)

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.

0 Kudos
Message 4 of 8
(3,878 Views)

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.

Message 5 of 8
(3,875 Views)

Hi Inigo,

 

it works for me, it only truncates the values without rounding.

Your code also does some rounding: positive values are rounded down, negative ones are rounded up!

Why do you need so much duplicated code?

check.png

 

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
Message 6 of 8
(3,870 Views)

@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. 😉

 

 

truncate towards zero with two decimal digits.png

 

 

You can even combine the last two functions into a compound node if you want (don't forget to invert):

 

truncate towards zero with two decimal digitsB.png

0 Kudos
Message 7 of 8
(3,863 Views)

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.

0 Kudos
Message 8 of 8
(3,853 Views)