Skip to main content
18-Opal
Jul 22, 2024 12:44 AM UTC
Solved

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.

terryhendicott
21-Topaz II
Jul 22, 2024 1:08 AM UTC

Hi,

Like this using two functions.

Capture.JPG

Cheers

Terry

4 replies

terryhendicott
21-Topaz II
Jul 22, 2024 1:08 AM UTC

Hi,

Like this using two functions.

Capture.JPG

Cheers

Terry

Translate
Werner_E
25-Diamond I
Jul 22, 2024 1:33 AM UTC

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

 

Translate
18-Opal
Jul 22, 2024 2:47 AM UTC
Just positive
Translate
Werner_E
25-Diamond I
Jul 22, 2024 10:47 AM UTC

@ppal_255687 wrote:
Just positive

Then you can use any of the five suggestions.

Translate