LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How to Round to Nearest Single precision Float

Solved!
Go to solution

HI everyone i use labview 2019 I have a single precision float number which is "0.598" I need the number to be rounded to the value "0.600" or if it's "0.453" it becomes "0.500" or if it's "0.803" it becomes "0.800" and so on. Can someone help me?

0 Kudos
Message 1 of 5
(820 Views)
Solution
Accepted by topic author a.garciaperez

One possibility. Multiply by 10, round to nearest integer, divide by 10.

 

(note that if you would display many more digits, they might not all be zero due to the limitations of floating point numbers and how they are represented in binary)

Message 2 of 5
(817 Views)

Perfect but is there a way to get even closer? like from "0.145" becomes "0.150" or "0.639" becomes "0.650" or "0.696" becomes "0.700"

0 Kudos
Message 3 of 5
(791 Views)
Solution
Accepted by topic author a.garciaperez

Hi a.,

 


@a.garciaperez wrote:

Perfect but is there a way to get even closer? like from "0.145" becomes "0.150" or "0.639" becomes "0.650" or "0.696" becomes "0.700"


So you want to round to multiples of 0.05? Multiply by 20…

 

Please define "getting closer" when it comes to simple math!

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
Message 4 of 5
(768 Views)

So basically you want to quantize the data to multiples of some fractional decimal value (that you never really define except with showing a very limited set of examples). As Gerd showed, the "multiply...round to nearest...divide" of course works with any step size.

 

As I already said, keep in mind that many of these simple decimal values (e.g. 0.1) don't have exact binary representation. Also be aware that LabVIEW uses bankers rounding, so if you want anything different for values that are exactly halfway, you need to work around that with a little bit more code.

Message 5 of 5
(726 Views)