Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Easy way to round up last variable
#1
Hi,

I have a variable that I need to truncate to 5 numbers plus a decimal point from a larger number in a string. The 6th digit of the should be rounded.

The problem is that the decimal point is not always in the same place so I can't easily use the



Code:
Copy      Help
VARIANT v.round(sample string 3);; rounds to 3 decimal places

command

Some number examples are as follows:

310.417389
484.000000
180.000000
50.027470
6.547363

My strategy so far has been to extract the first 7 digits (6 digits plus the decimal point).
I would like to round up the 7th digit(it won't ever be the decimal point)
and then get the first 6 digits (which will be what I need 5 digits + a decimal point)

But I don't know how to just round up a number (regardless of its decimal location)

I thought about reducing the number to an integer using Variant.fix

Code:
Copy      Help
str first7.left(sample number 7)
double first7=val(first7 2);; converts first 6 characters into a double variable
Variant v.fix(first7)


and then getting the length of the integer and then depending on that making individual regex's to get out the right number
but that seems too cumbersome, Especially if there is a way to round up last digit of any number regardless of its decimal location.

Thanks for any advice.

Stuart
#2
See also the format function in QM help. Although not sure, I think it allows to format doubles with specified number of digits.


Forum Jump:


Users browsing this thread: 1 Guest(s)