NI TestStand

cancel
Showing results for 
Search instead for 
Did you mean: 

Rounding positive and negative numbers using the Round function

Solved!
Go to solution

I am trying to round a calculated value. This value can be both positive and negative numbers. I want the posative numbers to round normally (at 0.5, round up to the nearest whole number) and negative numbers to work opposite (-25.5 rounds to -26) and I have not been able to figure it out. 

 

I have attached the current rounding code but when i get a value for locals.offset of -25.5, instead of rounding up (or down really) to -26, it rounds to -25. I understand why it would make sense for this to happen but I have not been able to get -25.5 to round to -26. Would somebody be able to help me out with this?

0 Kudos
Message 1 of 5
(5,070 Views)

Can you use one of the rounding options?  For example, instead of "round(-25.5)", use "round(-25.5,4)".  The 4 on the end directs the round function to always go to the nearest integer.

 

Pulido Technologies LLC

0 Kudos
Message 2 of 5
(5,032 Views)

Hi,

 

Use Round(x, 1), as the '1' for the second parameter will always round away from zero.  Positive numbers round up, negative numbers round down.

 

So...

 

Round(0.5, 1) = 1

Round(-25.5, 1) = -26

 

I hope this helps,

 

Charlie Rodway | Principal Software Engineer | Certified TestStand Architect (CTA)

Computer Controlled Solutions Ltd | NI Silver Alliance Partner | GDevCon#1 Sponsor

0 Kudos
Message 3 of 5
(4,993 Views)
Solution
Accepted by topic author KovAcevich7

but then you run into problems when you have -25.4 or 25.4 and you want to round to -25 and 25 respectively. 

 

I found that the error was in the calculation. By using a calculator or excel, numbers and equation I was using came out to exactly 25.5 but in TestStand it was something like 25.49999999999992300. I didn't see this until I found out how to increase the number of digits shown for a number variable. 

 

It wasn't a TestStand error, it was a calculation error. 

0 Kudos
Message 4 of 5
(4,986 Views)

Hi guys,

 

i need one solution regarding Range Property,

 

Instead of 0.5 range, i want 1 range.

 

example: if our range can be 65. nd if we get 64 means it should show 65 only and in other case if we get 66 means it should show 65 only.

 

can we do that in teststand.???

Sravankumar Atla
CLD || CTD
0 Kudos
Message 5 of 5
(3,724 Views)