34
\$\begingroup\$

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

Sandbox Link

\$\endgroup\$
4
  • \$\begingroup\$ 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? \$\endgroup\$
    – Jo King
    Sep 4, 2019 at 3:08
  • \$\begingroup\$ @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 \$\endgroup\$
    – Value Ink
    Sep 4, 2019 at 9:13
  • \$\begingroup\$ Feels good to be inspirational :) \$\endgroup\$
    – miike3459
    Sep 4, 2019 at 22:26
  • \$\begingroup\$ 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 \$\endgroup\$
    – Jo King
    Sep 4, 2019 at 23:48

39 Answers 39

1
2
1
\$\begingroup\$

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.

\$\endgroup\$
0
\$\begingroup\$

Perl 6, 19 bytes

{$_+|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

\$\endgroup\$
0
\$\begingroup\$

Java (OpenJDK 8), 43 bytes

a->{return(int)((int)a/a<1?a>0?a+1:a-1:a);}

Try it online!

\$\endgroup\$
4
  • 3
    \$\begingroup\$ The lambda function can be written without using an explicit return statement. \$\endgroup\$
    – Joel
    Sep 3, 2019 at 23:58
  • \$\begingroup\$ @Joel is indeed right. And you can save 4 additional bytes changing the (int)a/a<1 to a%1!=0: 30 bytes \$\endgroup\$ Sep 4, 2019 at 6:17
  • \$\begingroup\$ There's also the BigDecimal.setScale method that gives excellent results, like my answer demonstrates \$\endgroup\$ Sep 4, 2019 at 11:44
  • \$\begingroup\$ 33 bytes \$\endgroup\$
    – ceilingcat
    Nov 8, 2019 at 22:03
0
\$\begingroup\$

Japt, 6 bytes

Î*Ua c

Try it here

\$\endgroup\$
0
\$\begingroup\$

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]
\$\endgroup\$
0
\$\begingroup\$

Scala, 16 bytes

_.setScale(0,UP)

Try it online!

Scala, 22 bytes

x=>x.signum*x.abs.ceil

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Zsh, 43 36 35 bytes

Try it online! Try it online! Try it online!

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
\$\endgroup\$
0
\$\begingroup\$

APL(Dyalog Unicode), 5 bytes SBCS

××⌈∘|

Try it on APLgolf!

A train submission which takes a number on the right. Uses recursive's idea.

\$\endgroup\$
0
\$\begingroup\$

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
\$\endgroup\$
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.