08-13-2016 09:07 AM
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?
08-13-2016 09:35 AM
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.
08-13-2016 09:44 AM
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
08-13-2016 10:04 AM
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:
Bob Schor
P.S. -- I programmed this exact algorithm in Pascal about two decades ago. Loved it.
08-13-2016 01:46 PM
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.