Round away from zeroeig36

14
\\$\\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.

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

share|improve this question
\\$\\endgroup\\$
  • \\$\\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 3 hours ago

17 Answers 17

active oldest votes
5
\\$\\begingroup\\$

Jelly, 4 bytes

ĊṠ¡Ḟ

A monadic Link accepting a number which yields an integer.

Try it online! Or see a test-suite.

How?

ĊṠ¡Ḟ - Link: number, N
  ¡  - repeat...
 Ṡ   - ...number of times: sign of N (repeating -1 is the same as 0 times)
Ċ    - ...action: ceiling
   Ḟ - floor (that)
share|improve this answer
\\$\\endgroup\\$
  • \\$\\begingroup\\$ So how exactly does ¡ work for negative numbers? I don't think it's documented \\$\\endgroup\\$ – caird coinheringaahing 8 hours ago
  • 1
    \\$\\begingroup\\$ It's not documented on Jelly's wiki, but ¡s repetitive nature is implemented with a for index in range(repetitions) loop in the code. range([stop=]-1) is empty since start defaults to 0 and step defaults to 1 and "For a positive step, the contents of a range r are determined by the formula r[i] = start + step*i where i >= 0 and r[i] < stop." docs \\$\\endgroup\\$ – Jonathan Allan 7 hours ago
  • \\$\\begingroup\\$ ¡'s behavior relies on that of Python's range, and range(-1).__iter__().__next__() immediately throws StopIteration. \\$\\endgroup\\$ – Unrelated String 7 hours ago
4
\\$\\begingroup\\$

R, 32 bytes

x=scan()
sign(x)*ceiling(abs(x))
share|improve this answer
\\$\\endgroup\\$
  • \\$\\begingroup\\$ 31 bytes -- very nice answer! \\$\\endgroup\\$ – Giuseppe 8 hours ago
4
\\$\\begingroup\\$

Jelly, 5 bytes

Ṡ×AĊ$

Try it online!

This ports recursive's Stax answer into Jelly

Jelly, 6 bytes

ĊḞṠ‘$?

Try it online!

I feel like there is a shorter way than this, but it works, and isn't a port of another answer.

share|improve this answer
\\$\\endgroup\\$
  • \\$\\begingroup\\$ ĊḞ>?0 would work as your 6 does. \\$\\endgroup\\$ – Jonathan Allan 7 hours ago
  • 1
    \\$\\begingroup\\$ AĊ×Ṡ is 4 and functionally identical to your first answer. \\$\\endgroup\\$ – Nick Kennedy 7 hours ago
2
\\$\\begingroup\\$

JavaScript (ES6), 20 bytes

n=>n%1?n<0?~-n:-~n:n

Try it online!

share|improve this answer
\\$\\endgroup\\$
2
\\$\\begingroup\\$

Stax, 6 bytes

å├╪∙Bß

Run and debug it

Procedure:

  1. Absolute value
  2. Ceiling
  3. Multiply by original sign
share|improve this answer
\\$\\endgroup\\$
2
\\$\\begingroup\\$

Python 3, 24 bytes

lambda i:i-i%(1,-1)[i>0]

Try it online!

share|improve this answer
\\$\\endgroup\\$
2
\\$\\begingroup\\$

Retina 0.8.2, 38 bytes

\\.0+
.
\\b9+\\..
0$&
T`9d`d`.9*\\..
\\..*

Try it online! Link includes test cases. Explanation:

\\.0+
.

Delete zeroes after the decimal point, to ensure that the number is not an integer; the next two matches fail if there are no digits after the decimal point.

\\b9+\\..
0$&

If the integer part is all 9s, prefix a 0 to allow the increment to overflow.

T`9d`d`.9*\\..

Increment the integer part of the number.

\\..*

Delete the fractional part of the number.

share|improve this answer
\\$\\endgroup\\$
1
\\$\\begingroup\\$

Vim, 36 bytes/keystrokes

:s/-/-<Space>
:g/\\..*[1-9]/norm <C-v><C-a>lD
:s/<Space><cr>

Try it online! or Verify all Test Cases!

share|improve this answer
\\$\\endgroup\\$
1
\\$\\begingroup\\$

Runic Enchantments, 18 bytes

i:'|A:1+µ-'fA},*@

Try it online!

"Adds" (away from zero) 0.999999 and floors the result. µ is the closest thing to an infinitesimal in language's operators.

share|improve this answer
\\$\\endgroup\\$
  • 1
    \\$\\begingroup\\$ This outputs nothing for 0 \\$\\endgroup\\$ – Jo King 6 hours ago
  • 1
    \\$\\begingroup\\$ @JoKing Oof. Good catch. It's doing a divide by input to get the "sign" of the input value, which of course, divides by 0 when the input is 0. There's no (good) way around that right now. Will need this commit first. I'll poke Dennis (side benefit, the answer will get shorter). \\$\\endgroup\\$ – Draco18s 5 hours ago
1
\\$\\begingroup\\$

Perl 6, 18 bytes

{$_-.abs%-1*.sign}

Try it online!

share|improve this answer
\\$\\endgroup\\$
1
\\$\\begingroup\\$

Excel, 13 bytes

=ROUNDUP(A1,)

Alternative

=EVEN(A1*2)/2
share|improve this answer
\\$\\endgroup\\$
1
\\$\\begingroup\\$

J, 6 bytes

**>.@|

Try it online!

Just a 1 character change from my answer on the cousin question.

share|improve this answer
\\$\\endgroup\\$
0
\\$\\begingroup\\$

Brachylog, 7 bytes

⌋₁ℤ₁|⌉₁

Try it online!

or ⌉₁ℕ₁|⌋₁.

⌋₁         The input rounded down
  ℤ₁       is an integer less than -1
    |      and the output, or, the input
     ⌉₁    rounded up is the output.
share|improve this answer
\\$\\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

share|improve this answer
\\$\\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!

share|improve this answer
\\$\\endgroup\\$
  • 1
    \\$\\begingroup\\$ The lambda function can be written without using an explicit return statement. \\$\\endgroup\\$ – Joel 6 hours ago
  • \\$\\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\\$ – Kevin Cruijssen 52 secs ago
0
\\$\\begingroup\\$

Perl 5 -pF/\\./, 24 bytes

$_&&=$F[0]+$_*(@F-1)/abs

Try it online!

share|improve this answer
\\$\\endgroup\\$
0
\\$\\begingroup\\$

C, 94 bytes

#include<math.h>
main(){float a;scanf("%e",&a);printf("%f",(-2*signbit(a)+1)*ceil(fabs(a)));}

I compiled with

gcc a.c
share|improve this answer
New contributor
user1475369 is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
\\$\\endgroup\\$

Your Answer

If this is an answer to a challenge…

  • …Be sure to follow the challenge specification. However, please refrain from exploiting obvious loopholes. Answers abusing any of the standard loopholes are considered invalid. If you think a specification is unclear or underspecified, comment on the question instead.

  • …Try to optimize your score. For instance, answers to code-golf challenges should attempt to be as short as possible. You can always include a readable version of the code in addition to the competitive one. Explanations of your answer make it more interesting to read and are very much encouraged.

  • …Include a short header which indicates the language(s) of your code and its score, as defined by the challenge.

More generally…

  • …Please make sure to answer the question and provide sufficient detail.

  • …Avoid asking for help, clarification or responding to other answers (use comments instead).

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 code-golf number or ask your own question.

Ww ofh0 ate9vee5erh1 rneaee Go thhet EDmaced6ds mme

Popular posts from this blog

Coll del Lys (% Pa% 2deri lII

NG gemeente Greykerkda

पारमेनीडेस,hK9AaiUu,tgup 1 TreO Uu Ltomh ua12jnn