cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Need help navigating or using the PTC Community? Contact the community team. X

Round down Integer to odd number.

ppal
17-Peridot

Round down Integer to odd number.

Lets say I have an integer

 

If even number then round down to an odd number.

If odd then keep the number.

eg. x = 888

 

I want to round this  down to an odd number. 887

 

Thank you.

1 ACCEPTED SOLUTION

Accepted Solutions

Hi,

Like this using two functions.

Capture.JPG

Cheers

Terry

View solution in original post

4 REPLIES 4

Hi,

Like this using two functions.

Capture.JPG

Cheers

Terry

Werner_E
24-Ruby V
(To:ppal)

Should this also apply to zero and negative integers?

At least that's  what your description demands and what Terry's solution provides.

BTW, if your inputs are only integers, you may also omit the "floor" functions:

Werner_E_0-1721613775123.png

 

If you intend to use the function for positive integers only, you may also try this:

Werner_E_1-1721613856746.png

Note the capital "R" in "Round". You see that the result for negative odd inputs is 'wrong'.

 

In case you rather intend to round "in the direction of zero" (basically apply the rounding to the absolute value of the input) you may use

Werner_E_4-1721614097105.png

Of course you get the same result with

Werner_E_3-1721614084135.png

 

And at last here is an alternative function doing exactly what you demanded (and what Terry's function also provides)

Werner_E_5-1721614584651.png

 

ppal
17-Peridot
(To:Werner_E)

Just positive
Werner_E
24-Ruby V
(To:ppal)


@ppal wrote:
Just positive

Then you can use any of the five suggestions.

Top Tags