LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How do you read the number of digits a number has.

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.

 

0 Kudos
Message 1 of 14
(3,041 Views)

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.,)

Santhosh
Soliton Technologies

New to the forum? Please read community guidelines and how to ask smart questions

Only two ways to appreciate someone who spent their free time to reply/answer your question - give them Kudos or mark their reply as the answer/solution.

Finding it hard to source NI hardware? Try NI Trading Post
Message 2 of 14
(3,036 Views)

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?

 

 

 

0 Kudos
Message 3 of 14
(3,025 Views)

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?

 

  • How are your numbers formatted to string?
  • Do they come with a sign char (+/-)?
  • Do they come with scientific formatting including an exponent (like e+03) at the end? How do you define digits after the decimal point for such numbers?
  • Do your numbers always use the point as decimal delimiter or are other chars possible (like German comma)?

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!?

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 4 of 14
(3,023 Views)

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) 


"Should be" isn't "Is" -Jay
0 Kudos
Message 5 of 14
(3,008 Views)

You could do something like this (it is implicitly constrained for <1 but maybe that works for you): 

 

snip.png

______________________________________________________________
Have a pleasant day and be sure to learn Python for success and prosperity.
0 Kudos
Message 6 of 14
(3,001 Views)

@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.

 

altenbach_0-1650393134488.png

 

Start by attaching a simple example containing your input and some example values and we will give more specific advice. 😄

0 Kudos
Message 7 of 14
(2,991 Views)

This one is only constrained by the double itself ( in that it can't represent a number like 9755234386876.875875897090756) 

snip.png

______________________________________________________________
Have a pleasant day and be sure to learn Python for success and prosperity.
0 Kudos
Message 8 of 14
(2,998 Views)

altenbach_0-1650393134488.png

 

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*

0 Kudos
Message 9 of 14
(2,956 Views)

Lexical Class (under Comparison palette). 3 denotes digits 0-9.

 

-AK2DM

~~~~~~~~~~~~~~~~~~~~~~~~~~
"It’s the questions that drive us.”
~~~~~~~~~~~~~~~~~~~~~~~~~~
Message 10 of 14
(2,949 Views)