# Round away from zero

Inspired by Round towards zero.

Given a number input via any reasonable method, round the number "away from zero" - positive numbers round up, and negative numbers round down.

If you intend to take the input as a string (via STDIN, for example), you should be able to handle numbers with or without the decimal point. If you take it as a number, it should at least be able to handle floating-point precision (double precision not required) or rational numbers.

You can output a floating-point number with the decimal point (e.g. 42.0) if desired. (Or even have some test cases output floating-point and some output integer, if it makes your answer shorter.)

Standard loopholes are not allowed, etc etc.

## Test cases

-99.9 => -100
-33.5 => -34
-7    => -7
-1.1  => -2
0     => 0
2.3   => 3
8     => 8
99.9  => 100
42.0  => 42
-39.0 => -39


• if we're taking numbers in a string context, such as STDIN, do we need to support the .0 as the test cases seem to suggest?
– Jo King
Commented Sep 4, 2019 at 3:08
• @JoKing yes-- I will update the question to clarify. This was actually the original case, but then people in the sandbox suggested adding non-decimal test cases so uh, here we are with both, sorry Commented Sep 4, 2019 at 9:13
• Feels good to be inspirational :) Commented Sep 4, 2019 at 22:26
• It's funny that all the languages that did so well on the previous challenge by taking integer input and integer output won't work so well, since they can't tell the difference between -0.1 and 0.1
– Jo King
Commented Sep 4, 2019 at 23:48

# Risky, 20 bytes

_?{0-1?{0/_?{1+_0+0+1?{0+_0+0+_0+0+_0+0


Try it online!

Takes a pair representing a rational number.

{$_+|0+.sign*?/\./}  Try it online! Not the shortest solution, but I'm working on it. Basically this truncates the number, then adds one away from zero if the number was not whole to begin with # Java (OpenJDK 8), 43 bytes a->{return(int)((int)a/a<1?a>0?a+1:a-1:a);}  Try it online! • The lambda function can be written without using an explicit return statement. – Joel Commented Sep 3, 2019 at 23:58 • @Joel is indeed right. And you can save 4 additional bytes changing the (int)a/a<1 to a%1!=0: 30 bytes Commented Sep 4, 2019 at 6:17 • There's also the BigDecimal.setScale method that gives excellent results, like my answer demonstrates Commented Sep 4, 2019 at 11:44 • 33 bytes Commented Nov 8, 2019 at 22:03 # Japt, 6 bytes Î*Ua c  Try it here # Red, 46 bytes func[n][(pick[0 1]n = t: to 0 n)*(sign? n)+ t]  Try it online! The slightly more readable version is 2 byte longer (those long function names!): func[n][to 1(sign? n)* round/ceiling absolute n]  # Scala, 16 bytes _.setScale(0,UP)  Try it online! # Scala, 22 bytes x=>x.signum*x.abs.ceil  Try it online! # Zsh, 4336 35 bytes Using nested ternaries, for extra confusion. XOR logic from the round to 0 solution. x=$1
<<<$[(x^0-x?x+(x<0?-1:1):x)^0]  Explanation: x=$1                                    #set x
x^0-x?                         #if x has decimals (shorter than x^0!=x)
#(0 is falsy in this context)
x+(x<0?-1:1)        #  if x<0 subtract 1 else add 1
:x      #otherwise the result is just x
<<<$[( )^0] #truncate to int & output  # APL(Dyalog Unicode), 5 bytes SBCS ××⌈∘|  Try it on APLgolf! A train submission which takes a number on the right. Uses recursive's idea. # MMIX, 16 bytes (4 instrs) 40000002 17000200 17000300 F8010000  # Disassembly / Explanation rfz BN$0,0F              // if negative, skip next instruction
FINT $0,ROUND_UP,$0     // round to integer towards +inf
0H  FINT $0,ROUND_DOWN,$0   // round to -inf, no effect if already rounded
POP  1,0                // return