Round away from zeroeig36
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
17 Answers
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)
-
\\$\\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 afor index in range(repetitions)
loop in the code.range([stop=]-1)
is empty sincestart
defaults to0
andstep
defaults to1
and "For a positive step, the contents of a ranger
are determined by the formular[i] = start + step*i
wherei >= 0
andr[i] < stop
." docs \\$\\endgroup\\$ – Jonathan Allan 7 hours ago -
\\$\\begingroup\\$
¡
's behavior relies on that of Python'srange
, andrange(-1).__iter__().__next__()
immediately throwsStopIteration
. \\$\\endgroup\\$ – Unrelated String 7 hours ago
R, 32 bytes
x=scan()
sign(x)*ceiling(abs(x))
-
\\$\\begingroup\\$ 31 bytes -- very nice answer! \\$\\endgroup\\$ – Giuseppe 8 hours ago
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.
-
\\$\\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
JavaScript (ES6), 20 bytes
n=>n%1?n<0?~-n:-~n:n
Try it online!
Stax, 6 bytes
å├╪∙Bß
Run and debug it
Procedure:
- Absolute value
- Ceiling
- Multiply by original sign
Python 3, 24 bytes
lambda i:i-i%(1,-1)[i>0]
Try it online!
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 9
s, 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.
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!
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.
-
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
Perl 6, 18 bytes
{$_-.abs%-1*.sign}
Try it online!
Excel, 13 bytes
=ROUNDUP(A1,)
Alternative
=EVEN(A1*2)/2
J, 6 bytes
**>.@|
Try it online!
Just a 1 character change from my answer on the cousin question.
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.
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
Java (OpenJDK 8), 43 bytes
a->{return(int)((int)a/a<1?a>0?a+1:a-1:a);}
Try it online!
-
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
toa%1!=0
: 30 bytes \\$\\endgroup\\$ – Kevin Cruijssen 52 secs ago
Perl 5 -pF/\\./
, 24 bytes
$_&&=$F[0]+$_*(@F-1)/abs
Try it online!
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
.0
as the test cases seem to suggest? \\$\\endgroup\\$ – Jo King 3 hours ago