Round towards zeroeynkkheilaQqk
This is a simple task. Given a positive or negative real number, round it to the next whole integer closer to zero.
The challenge
Take input through any reasonable form (stdin, function, etc.) of one positive or negative real number.
Round this number "towards zero" - this means if it is positive you will round down, and if it is negative you will round up.
Return the number or output it to the console.
Test cases
1.1 => 1
-1.1 => -1
500.4 => 500
-283.5 => -283
50 => 50
-50 => -50
Rules
This is code-golf, so least score in bytes wins.
Be mindful of the standard loopholes.
Have fun! more Jimmy challenges coming soon
27 Answers
Python 3, 3 bytes
int
Try it online!
Truncates the digits after the decimal point
-
\\$\\begingroup\\$ This demonstrates that Python 3 is more popular than Python 2. \\$\\endgroup\\$ – A _ 3 hours ago
Python 2, 3 bytes
int
Try it online!
-
\\$\\begingroup\\$ Sorry, but I ninja'ed you (codegolf.stackexchange.com/a/190673/83048) \\$\\endgroup\\$ – MilkyWay90 8 hours ago
-
\\$\\begingroup\\$ Arg! But you're using Python 3... :) \\$\\endgroup\\$ – Chas Brown 8 hours ago
-
\\$\\begingroup\\$ True, people should know it works in Python 2 too \\$\\endgroup\\$ – MilkyWay90 8 hours ago
-
\\$\\begingroup\\$ Unfortunately you got bamboozled by about a minute :( \\$\\endgroup\\$ – connectyourcharger 8 hours ago
-
1\\$\\begingroup\\$ I wuz robbed! :) \\$\\endgroup\\$ – Chas Brown 8 hours ago
Perl 5 -p056l15, 2 bytes
<>
Try it online!
How does that work?
-056 # (CLI) Make "." the input record separator
-l15 # (CLI) Make "\\n" the output record separator
# (otherwise it would use the input separator)
-p # (CLI) Implicitly read $_ from STDIN
<> # Read the second input field and do nothing with it
-p # (CLI) Output $_ to STDOUT
Or if you prefer a more traditional answer:
Perl 5, 6 bytes
$_=int
Try it online!
-
\\$\\begingroup\\$ l15 isn't
\\n, it's\\r.\\nwould be l12. It looks the same in TIO, though. \\$\\endgroup\\$ – Grimy 18 mins ago
brainfuck, 26 bytes
,[.+++++[->+++++<]>+[,>]<]
Try it online!
Outputs with a trailing . if the number was a decimal
J, 6 bytes
**<.@|
Try it online!
Sign * times * the round down <. of the absolute value @|
Labyrinth & Hexagony, 3 bytes
Thanks to FryAmTheEggman for pointing out I'd written some Hexagony!
?!@
Try it online! & Try it online!
How?
Labyrinth and Hexagony will both tell you as early as possible!...
? - read and discard from STDIN until a digit, a - or a + is found. Then read as many characters as possible to form a valid (signed) decimal integer and push its value
! - pop a value and write its decimal representation to STDOUT
@ - exit the labyrinth
-
2\\$\\begingroup\\$ This may be true of some of Martin's other languages, but the exact same program works in Hexagony. \\$\\endgroup\\$ – FryAmTheEggman 6 hours ago
-
\\$\\begingroup\\$ Heh, I've always wanted to make an answer in Hexagony. Doing it without trying was the last thing I thought could happen! \\$\\endgroup\\$ – Jonathan Allan 6 hours ago
-
\\$\\begingroup\\$
IO@in Backhand works the same way, and&.@in Befunge. Probably a lot of languages with integer input, and only integer handling will be the same \\$\\endgroup\\$ – Jo King 6 hours ago -
\\$\\begingroup\\$ @JoKing So we are waiting for someone finding out a language with integer i/o and also reading all number from stdin to stack / list and then print them to stdout by default. I believe there could be one, and it would be an answer in zero bytes. \\$\\endgroup\\$ – tsh 2 hours ago
-
\\$\\begingroup\\$ @tsh most probably! \\$\\endgroup\\$ – Jonathan Allan 1 hour ago
Retina 0.8.2, 5 bytes
\\..*
Try it online! Link includes test cases.
Perl 6, 4 bytes
*+|0
Anonymous function.
Try it online!
Jelly, 1 byte
r
A full program (as a monadic Link it returns a list of length one).
Try it online!
How?
r - Main Link: number, X e.g. -7.999
r - inclusive range between left (X) and right (X) (implicit cast to integer of inputs)
- = [int(X):int(X)] = [int(X)] [-7]
- implicit (smashing) print -7
V (vim), 5 bytes
A.<esc>#D
Try it online!
Ruby, 11 bytes
proc &:to_i
I picked this one because it distinguishes itself from the lambdas that us Ruby golfers typically use (thankfully, it had the same bytecount as the "traditional" solution):
->n{n.to_i}
Try it online!
ReRegex, 12 bytes
\\..+//#input
Try it online!
ReRegex is a programming language which matches and replaces over and over until there are no matches.
MATCH
\\. The literal period/full stop char
.+ Followed by one or more characters
REPLACE
(nothing) Equivalent to removing the input
STRING TO REPEATEDLY MATCH/REPLACE UNTIL THERE ARE NO MATCHES
#input The input
Pip, 5 bytes
a//:1
Try it online!
Intel 8087 FPU machine code, 15 bytes
9B D9 2E 010D FLDCW CW_RNDZ ; modified CW register for round towards zero
D9 06 010F FLD A ; load single precision value A into ST(0)
DF 16 0113 FIST B ; store integer value of ST(0) into B
CW_RNDZ: ; control word for round towards zero
0F7F
Input is single precision value in a memory location A, output is integer value at memory location B.
The 8087 must first be put into round towards zero mode by setting the control word (0F7F). Then load the floating point value and store back to an integer.
JavaScript, 6 bytes
x=>x^0
Try it online!
JavaScript, 8 bytes
Using built in is 2 bytes longer...
parseInt
Try it online!
Triangular, 3 bytes
$.%
Try it online!
Triangular takes numeric input as an integer; any decimal values are truncated. If it's acceptable to just leave the input on the stack without printing it, then this solution can instead be:
Triangular, 1 byte
$
Try it online!
C (tcc), 39 bytes
I was actually quite surprised nobody thought of using C.
main(i){scanf("%d",&i);printf("%d",i);}
i is an integer, and taking input trunctuates the decimal value. Outputting as decimal retrieves that value.
TIO
Keg, 19 17 bytes
This outputs some trailing unprintable characters. (Now we need reversed input!)
?'(:\\.>')"(!2/|[,
Haskell, 8 bytes
truncate
Try it online!
A built-in that truncates the non-integer part of the number.
C# (Visual C# Interactive Compiler), 9 bytes
n=>(int)n
Try it online!
Wolfram Language (Mathematica), 11 bytes
IntegerPart
Try it online!
Ohm v2, 1 byte
ì
Try it online!
MathGolf, 1 byte
i
Try it online!
Casts the input to integer, using Python's int. As easy as it gets.
IBM/Lotus Notes Formula, 11 bytes
@Integer(i)
Takes input from a form field i. Only posted because of the fun feature that given a list the formula will be applied to all members of the list without the need of a @For loop and also because I haven't posted a Notes Formula answer for a while.
There is no online interpreter for Formula language so a screenshot showing output for all given test cases is the best I can do.

x86-64 Machine Code (Windows), 5 bytes
F3 0F 2C C0 cvttss2si eax,xmm0
C3 ret
cvttss2si - Convert with truncation scalar single to integer.
Pyth, 1 byte
s
Try it online!
Equivalent of Python's int(i) with implicit input and output.
Japt, 2 bytes
|0
Try it
The | operator to coerces the input value to an integer.
There may be a 1-byte solution, but I have not come up with it yet.
.on the output? \\$\\endgroup\\$ – Xcali 8 hours ago.? Or that the input won't start with a.? \\$\\endgroup\\$ – Jo King 6 hours ago.(as in50) and the input should not start with a.. \\$\\endgroup\\$ – connectyourcharger 6 hours ago