04-19-2022 12:14 PM
How do you read the number of digits a number has. For both decimal, and floating point numbers?
I want to be able to read a number and then know if it has 2 digits or 3 digits etc and how many digits are after the decimal point.
I am sure there is some easy way to do this in LabVIEW that I just don't know.
An example vi in LabVIEW 17 would be great.
Thanks for the help.
04-19-2022 12:20 PM
Do you get the number as a string? if yes, you can parse the number of characters to find out the number of digits.
If you get it as a numeric data type, there is no point in discussing the number of digits of precision as it is dependent on the data type (like SGL, DBL, EXT etc.,)
04-19-2022 12:45 PM - edited 04-19-2022 01:03 PM
For integers, this is trivially easy. (once you decide if you want decimal, octal, binary, or hexadecimal digits.)
For floating point numbers, the number of decimal digits after the decimal point (or comma) is most often an approximation, because exact decimal digits often have no corresponding value in the internal binary representation (e.g. 0.1 would require an infinite number of bits to correctly represent. The closest binary value probably has 15+ decimal digits. Not what you want!
You need to decide what is "close enough" to 3, 4, or 5, etc. decimal digits to be considered equal.
@RickDTS wrote:
I am sure there is some easy way to do this in LabVIEW that I just don't know.
There is nothing to "know"! You can figure that out by simply thinking about the problem. What have you tried? Do you have a simple VI showing your attempts and where you got stuck?
04-19-2022 12:47 PM
Hi Rick,
@RickDTS wrote:
I want to be able to read a number and then know if it has 2 digits or 3 digits etc and how many digits are after the decimal point.
What have you tried so far? Mind to attach your VI with some typical input data?
In the end it boils down to parsing an input string. I guess this is some kind of homework to learn about the MatchPattern string function!?
04-19-2022 01:01 PM
For integers and floats you can always round down and increment the result of log10 to get the place of the largest significant digit
To get the number of significant digits will be difficult for floats. But essentially do a round with precision in a loop until X*10^n = rnd(X*10^n)
04-19-2022 01:19 PM - edited 04-19-2022 01:20 PM
You could do something like this (it is implicitly constrained for <1 but maybe that works for you):
04-19-2022 01:33 PM
@RickDTS wrote:
How do you read the number of digits a number has. For both decimal, and floating point numbers?
You also want to specify the requirements better.
Is that "number" a formatted string or a numeric datatype?
Do you want to count leading and trailing zeroes?
Here's how to count the number of decimal digits in any string, but that's probably not what you really want.
Start by attaching a simple example containing your input and some example values and we will give more specific advice. 😄
04-19-2022 01:40 PM
This one is only constrained by the double itself ( in that it can't represent a number like 9755234386876.875875897090756)
04-19-2022 02:36 PM - edited 04-19-2022 02:46 PM
Can I ask what that function is?
Looks like string to byte array, somehow getting "3" out of the function if the byte represents an ascii number (??) then add the number of bytes that equated to 3, but I am not familiar with that function.
Edit: Found it. Lexical class function. Sweet. *hangs it on the tool belt*
04-19-2022 02:49 PM
Lexical Class (under Comparison palette). 3 denotes digits 0-9.
-AK2DM