LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

float to integer

What is the appropriate/standard method for converting from a double to int32 in Labview? 

 

I'm counting digits in a number say, "98.7" yields 9 tens, 8 ones, 7 tenths. 

Normally in "C" I would just do something like:

 

  ntens    = int(dblNum/10.0);

  dbltmp  = dblNum-10.0*ntens;

  nones    = int(dbltmp);

  ntenths  = int(10*(dbltmp-nones));

 

I've tried variousl methods in Labview, but there are always special cases to handle.

I can use the "Round towards -Infinity" but I'll have to check for positive numbers and it also appears to have round-off issue.  For example, when my number is "1.2", it maps it to "1.1".  I've used a probe to monitor the value of "2" going into the "Round towards -Infinity" with a "1" coming out, Labview 8.6.

 

I've read some of the other posts on this topic, but I've not seen a recommended procedures.

This seems like a pretty common thing to do .  Is there a simple way to do this? 

I've spent way too much time on this and it should be easy.

 

0 Kudos
Message 1 of 25
(26,384 Views)

I guess I can get around the roundoff issue

by changing the order of execution the calculation.

 

0 Kudos
Message 2 of 25
(26,371 Views)

Can you explain in a few more sentences what you are actually trying to do?

Do you simply want each digit as an I32 number? How many digits do you want?

 

 

You need to be aware that many fractional floating point numbers that are nice in decimal, cannot be exactly represented in binary, thus your 1.2 is actually 1.19999999999999996000, leading to the observed problem. This happens in any programming language.

 

You could:

Multiply the number with 10 first, round to nearest, convert to integer, and extract the three digits?

0 Kudos
Message 3 of 25
(26,362 Views)

To convert from float to integer, the I32 function you are using already does that.  It happens to round to nearest as well.  But the text of your message says you want more than that.  You want a round towards zero.

 

See this.

Message Edited by Ravens Fan on 08-16-2009 03:16 PM
0 Kudos
Message 4 of 25
(26,359 Views)
If you would prefer to avoid the vaguaries of floating point representations, try the following.
0 Kudos
Message 5 of 25
(26,350 Views)

>Can you explain in a few more sentences what you are actually trying to do?

>Do you simply want each digit as an I32 number? How many digits do you want?

 

I want an integer count of each digit.  My number ranges is from 0.1 - 99.9, so there are 3 digits, tens, ones, tenths.  I've already figured out a simple work around the limitation of the LabVIEW int32 conversion, (which rounds instead of truncates).  I just multiple my number by ten so the ranges is 1 - 999. Then I can work in integer math and I don't have any rounding errors.   Also I'm using the "Quotient & Remainder" function which includes a floor calculation.

 

0 Kudos
Message 6 of 25
(26,349 Views)

Try your code with a negative number and see if it makes sense.

0 Kudos
Message 7 of 25
(26,345 Views)

Regarding "GetDigits.vi", I know this will work, but

I'd rather avoid the conversion to ascii and parse the string just to find out the value of a digit.

 

0 Kudos
Message 8 of 25
(26,345 Views)

PointOnePa wrote:  I've used a probe to monitor the value of "2" going into the "Round towards -Infinity" with a "1" coming out, Labview 8.6.

 

 


That most likely means the value was below 2 but rounded in the probes display to 2 (1.9999999999999995 or something similar).

 

Such calculation are logarithmic based and should not be done with normal divides (in any language).

 

Here's a solution that should work for:

  • Any number
  • Any base (decimal is base 10)
  • Any precision


It returns an array for every position in the base writing system:

Base 10.png

 This was for base 10, here are the results for base 2 (binary):

Base2.png

 

Good luck,

 

Ton

Free Code Capture Tool! Version 2.1.3 with comments, web-upload, back-save and snippets!
Nederlandse LabVIEW user groep www.lvug.nl
My LabVIEW Ideas

LabVIEW, programming like it should be!
0 Kudos
Message 9 of 25
(26,340 Views)

I don't have any negative numbers in my application, so I'm not concerned with operation with negative numbers.

My input ranges is from 0.1 to 99.9. The input is a flow rate in ml/hour.

 

 

0 Kudos
Message 10 of 25
(26,338 Views)