LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

double to string conversion

How to  convert a double variable which doesn't have a fixed fractional part to string with exact the same appearance.

Is there any function to do this in Labview?

aa.jpg

0 Kudos
Message 1 of 5
(10,988 Views)

The problem with your request is that the value in your Numeric control will display the actual value based on the display format for the control, not based on the actual value stored.  Not all floating-point numbers can be exactly represented in memory, and you happened to select a good example: "0.123" is actually represented as "0.122999999999999998" in memory.  You can see this if you increase the digits of precision displayed by your control.

 

You can change your "Numeric" control from numeric to string, then convert it to double to work with the value.  If your numeric value is coming from another calculation, you'll have to jump through a few hoops to accomplish what you want.

Jim
You're entirely bonkers. But I'll tell you a secret. All the best people are. ~ Alice
For he does not know what will happen; So who can tell him when it will occur? Eccl. 8:7

0 Kudos
Message 2 of 5
(10,969 Views)

I think you can get close. Create a property node for the Numeric control. Select the Display Format property.  Wire the Format output to a case structure. Inside the various cases use the appropriate number to string conversion function. For some cases you will need to wire the precision property to the conversion function.

 

This will not work with the  automatic format and may not work exactly as you want if Hide trailing zeros is checked.

 

Lynn

0 Kudos
Message 3 of 5
(10,967 Views)

As the previous responders have noted, the problem with determining the "true" value of a Float is that (as the mathematicians would say), "except for a set of measure zero" (namely integers and multiples of 1/2^n) all have infinite binary expansions which are typically approximated as a 51-bit binary number, is deciding when/how to truncate the (typically) infinite fractional representation.  Now, if you looked at your floats with, say, 15 digits of precision, you might see "0.12300000000004623" or "0.1229999999999994623", both of which you might feel comfortable saying are really "0.123".  So that suggests the following algorithm:

  • As long as abs(floatnumber - integerpartof (floatnumber) < epsilon),
    • floatnumber = floatnumber * 10
  • At this point, floatnumber is accurately represented by an integer, so make it an Int.
  • The number of loops you went through (N) tells you where to place the decimal point.
  • Create an appropriate string with N digits to the right of the decimal point.

Bob Schor

 

P.S. -- I programmed this exact algorithm in Pascal about two decades ago.  Loved it.

 

Message 4 of 5
(10,958 Views)

If you simply want to see the same thing as in the control, look at the display format of the control, go to advanced format, and copy the format string you see there into your format string for the conversion.

 

(Be aware that the actual value in the control can be different outside the visible digits, so if you scan the resulting string back into a numeric, an equal comparison is likely to fail).

 

As others have already hinted, this is a good place to learn about floating point limitations. Since all values are binary internally, many exact decimal fractions would require an infinite number of bits in the binary mantissa.

 

 

Message 5 of 5
(10,942 Views)