Round away from zeroRound towards zeroCalculate the cube root of a numberCount the unique fractions with only integersBlock-diagonal matrix from columnsRound to n Sig FigsCreate a pyramidal matrixRound the stringConvert Between Percentages and DecimalRound like a ZeroGet the best of two ArraysRound towards zero
Spare the Dying during Rage Beyond Death
Can there be plants on the dark side of a tidally locked world?
My boss says "This will help us better view the utilization of your services." Does this mean my job is ending in this organisation?
Can a country avoid prosecution for crimes against humanity by denying it happened?
IEEE Registration Authority mac prefix
First Number to Contain Each Letter
Round away from zero
If I have an accident, should I file a claim with my car insurance company?
Did the US Climate Reference Network Show No New Warming Since 2005 in the US?
Do mortgage points get applied directly to the principal?
What does "se jouer" mean here?
Is the Levitate spell supposed to basically disable a melee-based enemy?
Do I need to get a noble in order to win Splendor?
How do I stop making people jump at home and at work?
What is the significance of 104% for throttle power and rotor speed?
When making yogurt, why doesn't bad bacteria grow as well?
Are treasury bonds more liquid than USD?
Tiny image scraper for xkcd.com
Why do we need explainable AI?
Is it rude to ask my opponent to resign an online game when they have a lost endgame?
Why did the VIC-II and SID use 6 µm technology in the era of 3 µm and 1.5 µm?
How to encode a class with 24,000 categories?
Adding transparency to ink drawing
Which is the best password hashing algorithm in .NET Core?
Round away from zero
Round towards zeroCalculate the cube root of a numberCount the unique fractions with only integersBlock-diagonal matrix from columnsRound to n Sig FigsCreate a pyramidal matrixRound the stringConvert Between Percentages and DecimalRound like a ZeroGet the best of two ArraysRound towards zero
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
$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
code-golf number
$endgroup$
add a comment |
$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
code-golf number
$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
add a comment |
$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
code-golf number
$endgroup$
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
code-golf number
code-golf number
edited 7 hours ago
DJMcMayhem♦
42.2k12 gold badges160 silver badges330 bronze badges
42.2k12 gold badges160 silver badges330 bronze badges
asked 9 hours ago
Value InkValue Ink
9,1987 silver badges36 bronze badges
9,1987 silver badges36 bronze badges
$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
add a comment |
$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
$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
$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
add a comment |
17 Answers
17
active
oldest
votes
$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)
$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 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
add a comment |
$begingroup$
R, 32 bytes
x=scan()
sign(x)*ceiling(abs(x))
$endgroup$
$begingroup$
31 bytes -- very nice answer!
$endgroup$
– Giuseppe
8 hours ago
add a comment |
$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.
$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
6 hours ago
add a comment |
$begingroup$
JavaScript (ES6), 20 bytes
n=>n%1?n<0?~-n:-~n:n
Try it online!
$endgroup$
add a comment |
$begingroup$
Stax, 6 bytes
å├╪∙Bß
Run and debug it
Procedure:
- Absolute value
- Ceiling
- Multiply by original sign
$endgroup$
add a comment |
$begingroup$
Python 3, 24 bytes
lambda i:i-i%(1,-1)[i>0]
Try it online!
$endgroup$
add a comment |
$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 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.
$endgroup$
add a comment |
$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!
$endgroup$
add a comment |
$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.
$endgroup$
1
$begingroup$
This outputs nothing for0
$endgroup$
– Jo King
6 hours ago
1
$begingroup$
@JoKing Oof. Good catch. It's doing adivide 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
add a comment |
$begingroup$
Perl 6, 18 bytes
$_-.abs%-1*.sign
Try it online!
$endgroup$
add a comment |
$begingroup$
Excel, 13 bytes
=ROUNDUP(A1,)
Alternative
=EVEN(A1*2)/2
$endgroup$
add a comment |
$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.
$endgroup$
add a comment |
$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$
add a comment |
$begingroup$
Java (OpenJDK 8), 43 bytes
a->return(int)((int)a/a<1?a>0?a+1:a-1:a);
Try it online!
$endgroup$
1
$begingroup$
The lambda function can be written without using an explicitreturn
statement.
$endgroup$
– Joel
6 hours ago
add a comment |
$begingroup$
Perl 5 -pF/./
, 24 bytes
$_&&=$F[0]+$_*(@F-1)/abs
Try it online!
$endgroup$
add a comment |
$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
New contributor
$endgroup$
add a comment |
$begingroup$
J, 6 bytes
**>.@|
Try it online!
Just a 1 character change from my answer on the cousin question.
$endgroup$
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "200"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f191241%2fround-away-from-zero%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
17 Answers
17
active
oldest
votes
17 Answers
17
active
oldest
votes
active
oldest
votes
active
oldest
votes
$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)
$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 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
add a comment |
$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)
$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 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
add a comment |
$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)
$endgroup$
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)
edited 8 hours ago
answered 8 hours ago
Jonathan AllanJonathan Allan
59.9k5 gold badges44 silver badges187 bronze badges
59.9k5 gold badges44 silver badges187 bronze badges
$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
add a comment |
$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
$begingroup$
So how exactly does
¡
work for negative numbers? I don't think it's documented$endgroup$
– caird coinheringaahing
8 hours ago
$begingroup$
So how exactly does
¡
work for negative numbers? I don't think it's documented$endgroup$
– caird coinheringaahing
8 hours ago
1
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$
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
$begingroup$
¡
's behavior relies on that of Python's range
, and range(-1).__iter__().__next__()
immediately throws StopIteration
.$endgroup$
– Unrelated String
7 hours ago
add a comment |
$begingroup$
R, 32 bytes
x=scan()
sign(x)*ceiling(abs(x))
$endgroup$
$begingroup$
31 bytes -- very nice answer!
$endgroup$
– Giuseppe
8 hours ago
add a comment |
$begingroup$
R, 32 bytes
x=scan()
sign(x)*ceiling(abs(x))
$endgroup$
$begingroup$
31 bytes -- very nice answer!
$endgroup$
– Giuseppe
8 hours ago
add a comment |
$begingroup$
R, 32 bytes
x=scan()
sign(x)*ceiling(abs(x))
$endgroup$
R, 32 bytes
x=scan()
sign(x)*ceiling(abs(x))
answered 8 hours ago
PunintendedPunintended
3461 silver badge4 bronze badges
3461 silver badge4 bronze badges
$begingroup$
31 bytes -- very nice answer!
$endgroup$
– Giuseppe
8 hours ago
add a comment |
$begingroup$
31 bytes -- very nice answer!
$endgroup$
– Giuseppe
8 hours ago
$begingroup$
31 bytes -- very nice answer!
$endgroup$
– Giuseppe
8 hours ago
$begingroup$
31 bytes -- very nice answer!
$endgroup$
– Giuseppe
8 hours ago
add a comment |
$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.
$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
6 hours ago
add a comment |
$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.
$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
6 hours ago
add a comment |
$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.
$endgroup$
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.
edited 8 hours ago
answered 9 hours ago
caird coinheringaahingcaird coinheringaahing
7,8833 gold badges31 silver badges88 bronze badges
7,8833 gold badges31 silver badges88 bronze badges
$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
6 hours ago
add a comment |
$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
6 hours ago
$begingroup$
ĊḞ>?0
would work as your 6 does.$endgroup$
– Jonathan Allan
7 hours ago
$begingroup$
ĊḞ>?0
would work as your 6 does.$endgroup$
– Jonathan Allan
7 hours ago
1
1
$begingroup$
AĊ×Ṡ
is 4 and functionally identical to your first answer.$endgroup$
– Nick Kennedy
6 hours ago
$begingroup$
AĊ×Ṡ
is 4 and functionally identical to your first answer.$endgroup$
– Nick Kennedy
6 hours ago
add a comment |
$begingroup$
JavaScript (ES6), 20 bytes
n=>n%1?n<0?~-n:-~n:n
Try it online!
$endgroup$
add a comment |
$begingroup$
JavaScript (ES6), 20 bytes
n=>n%1?n<0?~-n:-~n:n
Try it online!
$endgroup$
add a comment |
$begingroup$
JavaScript (ES6), 20 bytes
n=>n%1?n<0?~-n:-~n:n
Try it online!
$endgroup$
JavaScript (ES6), 20 bytes
n=>n%1?n<0?~-n:-~n:n
Try it online!
answered 8 hours ago
ArnauldArnauld
91.8k7 gold badges108 silver badges373 bronze badges
91.8k7 gold badges108 silver badges373 bronze badges
add a comment |
add a comment |
$begingroup$
Stax, 6 bytes
å├╪∙Bß
Run and debug it
Procedure:
- Absolute value
- Ceiling
- Multiply by original sign
$endgroup$
add a comment |
$begingroup$
Stax, 6 bytes
å├╪∙Bß
Run and debug it
Procedure:
- Absolute value
- Ceiling
- Multiply by original sign
$endgroup$
add a comment |
$begingroup$
Stax, 6 bytes
å├╪∙Bß
Run and debug it
Procedure:
- Absolute value
- Ceiling
- Multiply by original sign
$endgroup$
Stax, 6 bytes
å├╪∙Bß
Run and debug it
Procedure:
- Absolute value
- Ceiling
- Multiply by original sign
answered 8 hours ago
recursiverecursive
8,14615 silver badges32 bronze badges
8,14615 silver badges32 bronze badges
add a comment |
add a comment |
$begingroup$
Python 3, 24 bytes
lambda i:i-i%(1,-1)[i>0]
Try it online!
$endgroup$
add a comment |
$begingroup$
Python 3, 24 bytes
lambda i:i-i%(1,-1)[i>0]
Try it online!
$endgroup$
add a comment |
$begingroup$
Python 3, 24 bytes
lambda i:i-i%(1,-1)[i>0]
Try it online!
$endgroup$
Python 3, 24 bytes
lambda i:i-i%(1,-1)[i>0]
Try it online!
answered 8 hours ago
JitseJitse
1,4804 silver badges21 bronze badges
1,4804 silver badges21 bronze badges
add a comment |
add a comment |
$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 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.
$endgroup$
add a comment |
$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 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.
$endgroup$
add a comment |
$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 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.
$endgroup$
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.
answered 7 hours ago
NeilNeil
88.3k8 gold badges46 silver badges186 bronze badges
88.3k8 gold badges46 silver badges186 bronze badges
add a comment |
add a comment |
$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!
$endgroup$
add a comment |
$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!
$endgroup$
add a comment |
$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!
$endgroup$
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!
edited 8 hours ago
answered 8 hours ago
DJMcMayhem♦DJMcMayhem
42.2k12 gold badges160 silver badges330 bronze badges
42.2k12 gold badges160 silver badges330 bronze badges
add a comment |
add a comment |
$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.
$endgroup$
1
$begingroup$
This outputs nothing for0
$endgroup$
– Jo King
6 hours ago
1
$begingroup$
@JoKing Oof. Good catch. It's doing adivide 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
add a comment |
$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.
$endgroup$
1
$begingroup$
This outputs nothing for0
$endgroup$
– Jo King
6 hours ago
1
$begingroup$
@JoKing Oof. Good catch. It's doing adivide 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
add a comment |
$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.
$endgroup$
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.
edited 8 hours ago
answered 8 hours ago
Draco18sDraco18s
2,1837 silver badges21 bronze badges
2,1837 silver badges21 bronze badges
1
$begingroup$
This outputs nothing for0
$endgroup$
– Jo King
6 hours ago
1
$begingroup$
@JoKing Oof. Good catch. It's doing adivide 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
add a comment |
1
$begingroup$
This outputs nothing for0
$endgroup$
– Jo King
6 hours ago
1
$begingroup$
@JoKing Oof. Good catch. It's doing adivide 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
1
$begingroup$
This outputs nothing for
0
$endgroup$
– Jo King
6 hours ago
$begingroup$
This outputs nothing for
0
$endgroup$
– Jo King
6 hours ago
1
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
$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
add a comment |
$begingroup$
Perl 6, 18 bytes
$_-.abs%-1*.sign
Try it online!
$endgroup$
add a comment |
$begingroup$
Perl 6, 18 bytes
$_-.abs%-1*.sign
Try it online!
$endgroup$
add a comment |
$begingroup$
Perl 6, 18 bytes
$_-.abs%-1*.sign
Try it online!
$endgroup$
Perl 6, 18 bytes
$_-.abs%-1*.sign
Try it online!
answered 6 hours ago
nwellnhofnwellnhof
8,1701 gold badge12 silver badges29 bronze badges
8,1701 gold badge12 silver badges29 bronze badges
add a comment |
add a comment |
$begingroup$
Excel, 13 bytes
=ROUNDUP(A1,)
Alternative
=EVEN(A1*2)/2
$endgroup$
add a comment |
$begingroup$
Excel, 13 bytes
=ROUNDUP(A1,)
Alternative
=EVEN(A1*2)/2
$endgroup$
add a comment |
$begingroup$
Excel, 13 bytes
=ROUNDUP(A1,)
Alternative
=EVEN(A1*2)/2
$endgroup$
Excel, 13 bytes
=ROUNDUP(A1,)
Alternative
=EVEN(A1*2)/2
answered 4 hours ago
nwellnhofnwellnhof
8,1701 gold badge12 silver badges29 bronze badges
8,1701 gold badge12 silver badges29 bronze badges
add a comment |
add a comment |
$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.
$endgroup$
add a comment |
$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.
$endgroup$
add a comment |
$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.
$endgroup$
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.
answered 7 hours ago
Unrelated StringUnrelated String
3,5822 gold badges3 silver badges21 bronze badges
3,5822 gold badges3 silver badges21 bronze badges
add a comment |
add a comment |
$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$
add a comment |
$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$
add a comment |
$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$
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
answered 6 hours ago
Jo KingJo King
31.1k4 gold badges72 silver badges139 bronze badges
31.1k4 gold badges72 silver badges139 bronze badges
add a comment |
add a comment |
$begingroup$
Java (OpenJDK 8), 43 bytes
a->return(int)((int)a/a<1?a>0?a+1:a-1:a);
Try it online!
$endgroup$
1
$begingroup$
The lambda function can be written without using an explicitreturn
statement.
$endgroup$
– Joel
6 hours ago
add a comment |
$begingroup$
Java (OpenJDK 8), 43 bytes
a->return(int)((int)a/a<1?a>0?a+1:a-1:a);
Try it online!
$endgroup$
1
$begingroup$
The lambda function can be written without using an explicitreturn
statement.
$endgroup$
– Joel
6 hours ago
add a comment |
$begingroup$
Java (OpenJDK 8), 43 bytes
a->return(int)((int)a/a<1?a>0?a+1:a-1:a);
Try it online!
$endgroup$
Java (OpenJDK 8), 43 bytes
a->return(int)((int)a/a<1?a>0?a+1:a-1:a);
Try it online!
edited 6 hours ago
answered 6 hours ago
X1M4LX1M4L
1,0396 silver badges16 bronze badges
1,0396 silver badges16 bronze badges
1
$begingroup$
The lambda function can be written without using an explicitreturn
statement.
$endgroup$
– Joel
6 hours ago
add a comment |
1
$begingroup$
The lambda function can be written without using an explicitreturn
statement.
$endgroup$
– Joel
6 hours ago
1
1
$begingroup$
The lambda function can be written without using an explicit
return
statement.$endgroup$
– Joel
6 hours ago
$begingroup$
The lambda function can be written without using an explicit
return
statement.$endgroup$
– Joel
6 hours ago
add a comment |
$begingroup$
Perl 5 -pF/./
, 24 bytes
$_&&=$F[0]+$_*(@F-1)/abs
Try it online!
$endgroup$
add a comment |
$begingroup$
Perl 5 -pF/./
, 24 bytes
$_&&=$F[0]+$_*(@F-1)/abs
Try it online!
$endgroup$
add a comment |
$begingroup$
Perl 5 -pF/./
, 24 bytes
$_&&=$F[0]+$_*(@F-1)/abs
Try it online!
$endgroup$
Perl 5 -pF/./
, 24 bytes
$_&&=$F[0]+$_*(@F-1)/abs
Try it online!
answered 3 hours ago
XcaliXcali
6,8341 gold badge7 silver badges24 bronze badges
6,8341 gold badge7 silver badges24 bronze badges
add a comment |
add a comment |
$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
New contributor
$endgroup$
add a comment |
$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
New contributor
$endgroup$
add a comment |
$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
New contributor
$endgroup$
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
New contributor
New contributor
answered 3 hours ago
user1475369user1475369
113 bronze badges
113 bronze badges
New contributor
New contributor
add a comment |
add a comment |
$begingroup$
J, 6 bytes
**>.@|
Try it online!
Just a 1 character change from my answer on the cousin question.
$endgroup$
add a comment |
$begingroup$
J, 6 bytes
**>.@|
Try it online!
Just a 1 character change from my answer on the cousin question.
$endgroup$
add a comment |
$begingroup$
J, 6 bytes
**>.@|
Try it online!
Just a 1 character change from my answer on the cousin question.
$endgroup$
J, 6 bytes
**>.@|
Try it online!
Just a 1 character change from my answer on the cousin question.
answered 2 hours ago
JonahJonah
4,6622 gold badges12 silver badges22 bronze badges
4,6622 gold badges12 silver badges22 bronze badges
add a comment |
add a comment |
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).
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f191241%2fround-away-from-zero%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
$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