Skip to main content
18-Opal
July 22, 2024
Solved

Round down Integer to odd number.

  • July 22, 2024
  • 2 replies
  • 1547 views

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.

Best answer by terryhendicott

Hi,

Like this using two functions.

Capture.JPG

Cheers

Terry

2 replies

21-Topaz II
July 22, 2024

Hi,

Like this using two functions.

Capture.JPG

Cheers

Terry

25-Diamond I
July 22, 2024

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

 

18-Opal
July 22, 2024
Just positive
25-Diamond I
July 22, 2024

@ppal_255687 wrote:
Just positive

Then you can use any of the five suggestions.