05-19-2021 01:24 PM
I have a VI that when run, can return one of the various answers with a decimal format displayed below:
1.3661
13.661
136.61
1366.1
For any of these return values, I would like to format the value to return 4 digits as a whole number like this:
1336
Solved! Go to Solution.
05-19-2021 02:04 PM - edited 05-19-2021 02:31 PM
Hi Robert,
@robertgalusha wrote:
1.3661
13.661
136.61
1366.1
For any of these return values, I would like to format the value to return 4 digits as a whole number like this:
1336
What is the question?
What have you tried? Where are you stuck?
Are there other things to consider? (Like negative numbers with leading minus char!?)
What is the expected input range of those numeric values?
Ideas:
05-19-2021 02:28 PM
Are you trying to return the four significant digits? Is this some kind of homework exercise? (Note: if it is, it doesn't disqualify you from receiving a solution, but it may determine how we help you arrive at one.)
05-19-2021 02:54 PM
@robertgalusha wrote:
I have a VI that when run, can return one of the various answers with a decimal format displayed below:
1.3661
13.661
136.61
1366.1
For any of these return values, I would like to format the value to return 4 digits as a whole number like this:
1336
Just trying to figure out the pattern here, all of your numbers have 13661 with the decimal place moving around, but you want to return 1336. Is that right, or do you really want 1366?
Kind of brute force, but I would convert to fractional string, search and replace "." with "" (empty string), and get string subset of length 4.
05-19-2021 07:06 PM - edited 05-19-2021 07:10 PM
05-20-2021 07:11 AM
05-20-2021 07:54 AM
Ideas:
05-20-2021 08:11 AM
05-20-2021 10:23 AM
@robertgalusha wrote:
Ideas:
- Convert the number to string, replace the point by an empty char, keep the first 4 chars, (convert back to integer if needed)…
- Use some math to scale the numeric value to the range [1000, 9999], then round to next integer…
GerdW.....This worked & did what was needed.
You did not say what "this" is that worked (there are two suggestions!), but if you implemented the first option, it will fail if the input number is less than 1. You would also need to deal with leading zeroes, etc.
05-20-2021 01:41 PM
@thols wrote:
I would like to format the value to return 4 digits as a whole number like this:1336
I should wait for the "is this homework?" reply, but since Altenbach already posted a solution, I couldn't resist:
Are you sure it's not 1337 you want to return? In that case you can do like this:
I already furnished this response a few posts back. 😉