| Bytes | Lang | Time | Link |
|---|---|---|---|
| 020 | Raku Perl 6 rakudo | 250417T165241Z | xrs |
| 023 | Juby | 250408T153541Z | Jordan |
| 031 | AWK | 250408T151342Z | xrs |
| 020 | Risky | 210720T222747Z | xigoi |
| 004 | Vyxal r | 210501T054812Z | Aaroneou |
| 017 | Julia 1.0 | 210501T005919Z | Tanj |
| 048 | PowerShell Core | 210429T230109Z | Julian |
| 037 | PowerShell Core | 210430T051915Z | mazzy |
| 164 | MMIX | 210429T202647Z | NoLonger |
| 005 | APLDyalog Unicode | 210212T070902Z | Razetime |
| 005 | Husk | 201018T160838Z | LegionMa |
| 021 | JavaScript node.js | 190905T174817Z | Sagittar |
| 016 | Runic Enchantments | 190903T212459Z | Draco18s |
| 024 | C# Visual C# Compiler | 190904T141601Z | hessam h |
| 039 | C | 190904T030715Z | girobuz |
| 035 | Perl 5 pF/\./ | 190904T023144Z | Xcali |
| 030 | PHP | 190904T114026Z | Night2 |
| 1312 | sed | 190904T233703Z | Ray |
| 036 | Vim | 190903T212729Z | DJMcMayh |
| 015 | APL Dyalog Unicode | 190904T153522Z | J. Sall& |
| 005 | MathGolf | 190904T140114Z | maxb |
| 035 | Zsh | 190904T122653Z | roblogic |
| 057 | Factor | 190904T132444Z | Galen Iv |
| 016 | Scala | 190904T091511Z | Soapy |
| 018 | Java JDK | 190904T113235Z | Olivier |
| 018 | Perl 6 | 190903T235120Z | nwellnho |
| 018 | Wolfram Language Mathematica | 190904T080900Z | att |
| 020 | JavaScript ES6 | 190903T211348Z | Arnauld |
| 046 | Red | 190904T065901Z | Galen Iv |
| 004 | Jelly | 190903T211007Z | caird co |
| 023 | Python 3 | 190903T214527Z | Jitse |
| 006 | Japt | 190904T065240Z | Shaggy |
| 006 | 05AB1E | 190904T062447Z | Kevin Cr |
| 006 | J | 190904T032208Z | Jonah |
| 013 | Excel | 190904T012715Z | nwellnho |
| 043 | Java OpenJDK 8 | 190903T231954Z | X1M4L |
| 019 | Perl 6 | 190903T232826Z | Jo King |
| 038 | Retina 0.8.2 | 190903T222601Z | Neil |
| 007 | Brachylog | 190903T222144Z | Unrelate |
| 004 | Jelly | 190903T215435Z | Jonathan |
| 032 | R | 190903T213152Z | Puninten |
| 006 | Stax | 190903T212806Z | recursiv |
Raku (Perl 6) (rakudo), 20 bytes
{.sign*.abs.ceiling}
{.sign # returns 1/-1
* # multiply
.abs # absolute value
.ceiling} # round up
J-uby, 23 bytes
:*%[:abs|:ceil,~:<=>&0]
Explanation
:* % [ :abs | :ceil, ~:<=> & 0 ]
:* % [ , ] # Product of...
:abs | :ceil # ceiling of absolute value of input and
~:<=> & 0 # signum of input
AWK, 56 31 bytes
{$2&&$1+=$1>0?1:-1}1,$0=int($1)
{int(a=$0)!=a&&substr(a,length)&&a+=a>0?1:-1}1,$0=int(a)
{int(a=$0)!=a # if it's decimal
&&substr(a,length) # and not zero
&&a+=a>0?1:-1} # then round away
1, # force output for zero
$0=int(a) # set output to integer
Risky, 20 bytes
_?{0-1?{0/_?{1+_0+0+1?{0+_0+0+_0+0+_0+0
Takes a pair representing a rational number.
Vyxal r, 5 4 bytes
Built-ins I previously didn't know existed FTW!
ȧ⌈∆±
Explanation:
# Implicit input
ȧ # Absolute value
⌈ # Ceiling
∆± # Change to the sign of input
# Implicit output
Julia 1.0, 17 bytes
Save 6 bytes thanks to the awesome comments of MarcMush!
x->x+sign(x%=1)-x
An alternative with 22 bytes
x->sign(x)ceil(abs(x))
PowerShell Core, 54 48 bytes
$a,$u=$args-split'\.'
(1,-1)[2*!+$u+($a-lt0)]+$a
Thanks @mazzy for the improved golfing !
Original implementation
$a,$u="$args"|% s*t '.'
(((1,-1)[$a-lt0]+$a),$a)[!+$u]
Explanations
$a,$u="$args"|% s*t '.' # splits the input as a string using '.'
[!+$u] # if there are no decimals
( ,$a) # return the int part as is
((1,-1)[$a-lt0]+$a) # return the int part + or - one depending of the sign of the input
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
APL(Dyalog Unicode), 5 bytes SBCS
××⌈∘|
A train submission which takes a number on the right. Uses recursive's idea.
Husk, 5 bytes
*±¹⌈a
Try it online! Just takes the sign of the input times the ceiling of its absolute value.
JavaScript (node.js), 30 23 21 bytes
s=>~~s+Math.sign(s%1)
Inspired by the C# answer.
Thanks to @Value Ink and @Gust van de Wal for -7 bytes!
Thanks again, @Gust van de Wal for another -2 bytes!
Runic Enchantments, 18 16 bytes
1µ-i:'-A{*+'.A@
"Adds" (away from zero) 0.999999 and floors the result. µ is the closest thing to an infinitesimal in language's operators. With a properly functioning Trunc(x) command, answer now supports 0 as input.
C# (Visual C# Compiler), 41 bytes27 bytes24 bytes
s=>(int)s+Math.Sign(s%1)
First post here, had fun with it, hope u like it. Kinda felt C# place is empty here
-14 tnx to @expired data
-3 tnx to @night2
PHP, 30 bytes
<?=0^$argn-=0<=>fmod($argn,1);
If number is not integer, based on the sign -1 (for negative decimals) or 1 (for positive decimals) is added to it and then integer part of the new number is printed.
PHP, 32 bytes
<?=[ceil,floor][$argn<0]($argn);
Basically outputs floor of input if it is less than 0, else ceil of it.
PHP, 34 bytes
<?=($argn>0?:-1)*ceil(abs($argn));
sed, 131 bytes + 2 bytes for -r flag
/^-?[^.]*(\.0*)?$/bQ
s/^(-?)9/\109/
s/([0-8]9*)\..*$/_\1/
h
s/.*_//
y/0123456789/1234567890/
G
s/(.*)\n(.*)_(.*)/\2\1/
:Q
s/\..*$//
Ungolfed
#!/bin/sed -rf
# identify integers
/^-?[^.]*(\.0*)?$/ b isInt
# add a leading 0 if we'll need it later
s/^(-?)9/\109/
# convert to format: -?[0-9]_[0-8]9*
s/([0-8]9*)\..*$/_\1/
# move the part that will remain constant into the hold buffer
h
s/.*_//
# [0-8]9* can be incremented via character substitution
y/0123456789/1234567890/
# reassemble parts
G
s/(.*)\n(.*)_(.*)/\2\1/
:isInt
# Get rid of anything after the decimal point
s/\..*$//
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!
Explanation:
:s/ " Replace...
- " A dash
/-<Space> " With a dash and a space
:g/ " On Every line matching this regex...
\. " A dot
.* " Followed By anything
[1-9] " Followed by a digit other than 0
/norm " Run the following keystrokes...
<C-v><C-a> " Increment the number by 1
" This also conveniently places our cursor just before the dot
l " Move one character right
D " Delete everything after the cursor
:s/ " Replace...
<Space> " A space
" (With nothing)
APL (Dyalog Unicode), 15 bytes
{⍎'⌈⌊'[0>⍵],⍕⍵}
Simple Dfn. Uses ⎕IO←0.
How:
{⍎'⌈⌊'[0>⍵],⍕⍵} ⍝ Main function, argument ⍵.
⍕⍵ ⍝ Stringified argument
, ⍝ Appended to
[0>⍵] ⍝ This item... (0 if ⍵ is positive or 0, else 1)
'⌈⌊' ⍝ of this string (which are the functions Ceiling and Floor, respectively)
⍎ ⍝ Executed as APL code.
MathGolf, 5 bytes
‼σ±ü*
Explanation
It's nice to find usage for the ‼ operator.
‼ apply next two operators to (implicit) input
σ sign (-1, 0, or 1)
± absolute value
ü ceiling of that absolute value
* multiply the rounded absolute value with the sign
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
Java (JDK), 18 bytes
d->d.setScale(0,0)
Explanations
Uses a BigDecimal as input and output. BigDecimal has a method setScale that sets the scale of the number. The first parameter is the number of digits after the dot separator, the second is the rounding mode. ROUND_UP is the "away-from-zero" rounding and has a value of 0 so I hardcode that value.
Perl 6, 18 bytes
{$_-.abs%-1*.sign}
Explanation
{ } # Anonymous block
.abs # Absolute value
%-1 # Modulo -1
*.sign # Multiply by sign
$_- # Subtract from original
JavaScript (ES6), 20 bytes
n=>n%1?n<0?~-n:-~n:n
Commented
n => // n = input
n % 1 ? // if n is not an integer:
n < 0 ? // if n is negative:
~-n // return -(floor(-n) + 1) = -floor(-n) - 1
: // else:
-~n // return -(-(floor(n) + 1)) = floor(n) + 1
: // else:
n // return n unchanged
Red, 46 bytes
func[n][(pick[0 1]n = t: to 0 n)*(sign? n)+ t]
The slightly more readable version is 2 byte longer (those long function names!):
func[n][to 1(sign? n)* round/ceiling absolute n]
Jelly, 5 4 bytes
AĊ×Ṡ
This ports recursive's Stax answer into Jelly, so check that answer out for an explanation.
-1 byte thanks to Nick Kennedy
Jelly, 6 5 bytes
ĊḞ>?0
-1 byte thanks to Jonathan Allan
How this one works
ĊḞ>?0 - Monadic link. Takes a float, x, as argument
? - If:
> 0 - x > 0
- Then:
Ċ - ceil(x)
- Else:
Ḟ - floor(x)
05AB1E, 6 bytes
ÄîI.±*
A 5-byter should definitely be possible..
Try it online or verify all test cases.
Explanation:
Ä # The absolute value of the (implicit) input,
î # ceiled
* # And then multiplied by
.± # the signum
I # of the input
# (after which the result is output implicitly)
Excel, 13 bytes
=ROUNDUP(A1,)
Alternative
=EVEN(A1*2)/2
Perl 6, 19 bytes
{$_+|0+.sign*?/\./}
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
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.
Brachylog, 7 bytes
⌋₁ℤ₁|⌉₁
or ⌉₁ℕ₁|⌋₁.
⌋₁ The input rounded down
ℤ₁ is an integer less than -1
| and the output, or, the input
⌉₁ rounded up is the output.
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)
R, 32 bytes
x=scan()
sign(x)*ceiling(abs(x))