21
\$\begingroup\$

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.9   =>  1
-1.1   => -1
 500.4 =>  500
-283.5 => -283
 50    =>  50
-50    => -50

Rules

Have fun! more Jimmy challenges coming soon

\$\endgroup\$
16
  • 3
    \$\begingroup\$ May i output 3.00 for 3.14? \$\endgroup\$
    – tsh
    Commented Aug 23, 2019 at 5:33
  • 1
    \$\begingroup\$ @A_ If error messages are in stderr. And your output are in stdout. It is allowed by default. \$\endgroup\$
    – tsh
    Commented Aug 23, 2019 at 6:59
  • 2
    \$\begingroup\$ Hmm, this seems unreasonably trivial for a code golf. Most langs will have a builtin for this, no? It looks like we are to assume all input and output are strings? \$\endgroup\$
    – Octopus
    Commented Aug 23, 2019 at 19:13
  • 3
    \$\begingroup\$ 3.00 certainly is an integer. More precisely, in standard mathematical notation as well as in many programming languages, the notation "3.00" denotes the number 3, which is an integer; but in many programming languages, it indicates that the number is to be stored in a floating-point format. (But it's an integer regardless of the format it's stored in.) \$\endgroup\$ Commented Aug 23, 2019 at 20:06
  • 2
    \$\begingroup\$ Rounding towards zero, that is, discarding the fractional part, is called truncation and is a standard operation in almost all languages. Typically, doing this kind of rounding is more "basic" than doing a ceil or a floor. \$\endgroup\$ Commented Aug 24, 2019 at 15:00

71 Answers 71

43
\$\begingroup\$

Python 3, 3 bytes

int

Try it online!

Truncates the digits after the decimal point.

NOTE: This is a trivial answer. Please take a look at the other answers before upvoting

\$\endgroup\$
9
  • 23
    \$\begingroup\$ This demonstrates that Python 3 is more popular than Python 2. \$\endgroup\$
    – user85052
    Commented Aug 23, 2019 at 5:09
  • 2
    \$\begingroup\$ Err... Why the upvotes? This is a pretty trivial answer... \$\endgroup\$
    – MilkyWay90
    Commented Aug 23, 2019 at 16:34
  • \$\begingroup\$ I think it's your excellent explanation of the code. :) \$\endgroup\$
    – Chas Brown
    Commented Aug 23, 2019 at 22:37
  • 4
    \$\begingroup\$ @ChasBrown I don't think so... the explanation is not even a standard caliber explanation. \$\endgroup\$
    – MilkyWay90
    Commented Aug 23, 2019 at 23:08
  • \$\begingroup\$ I guess @Chas is pointing out that the explanation is infinitely more complete than his own. \$\endgroup\$
    – prl
    Commented Aug 26, 2019 at 0:46
23
\$\begingroup\$

Python 2, 3 bytes

int

Try it online!

\$\endgroup\$
6
  • 2
    \$\begingroup\$ Sorry, but I ninja'ed you (codegolf.stackexchange.com/a/190673/83048) \$\endgroup\$
    – MilkyWay90
    Commented Aug 22, 2019 at 23:36
  • 2
    \$\begingroup\$ Arg! But you're using Python 3... :) \$\endgroup\$
    – Chas Brown
    Commented Aug 22, 2019 at 23:37
  • \$\begingroup\$ True, people should know it works in Python 2 too \$\endgroup\$
    – MilkyWay90
    Commented Aug 22, 2019 at 23:38
  • \$\begingroup\$ Unfortunately you got bamboozled by about a minute :( \$\endgroup\$
    – moltarze
    Commented Aug 22, 2019 at 23:40
  • 5
    \$\begingroup\$ I wuz robbed! :) \$\endgroup\$
    – Chas Brown
    Commented Aug 22, 2019 at 23:42
14
\$\begingroup\$

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
\$\endgroup\$
14
\$\begingroup\$

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!

\$\endgroup\$
5
  • \$\begingroup\$ l15 isn't \n, it's \r. \n would be l12. It looks the same in TIO, though. \$\endgroup\$
    – Grimmy
    Commented Aug 23, 2019 at 7:56
  • \$\begingroup\$ for the second option, there's also -Minteger -p $_/=1 \$\endgroup\$ Commented Aug 23, 2019 at 10:32
  • 4
    \$\begingroup\$ The first solution is actually 8 bytes because you need to include the flags in your byte count \$\endgroup\$ Commented Aug 23, 2019 at 10:59
  • 2
    \$\begingroup\$ @JohnDvorak actually per the meta post codegolf.meta.stackexchange.com/questions/14337/…, flags don’t add bytes but count as a different version of the language. \$\endgroup\$ Commented Aug 23, 2019 at 13:14
  • \$\begingroup\$ @NahuelFouilleul I thought about that one, too, but it didn't matter since I got to 2 bytes the other way. \$\endgroup\$
    – Xcali
    Commented Aug 23, 2019 at 22:07
9
\$\begingroup\$

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
\$\endgroup\$
6
  • 3
    \$\begingroup\$ This may be true of some of Martin's other languages, but the exact same program works in Hexagony. \$\endgroup\$ Commented Aug 23, 2019 at 1:20
  • 4
    \$\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\$ Commented Aug 23, 2019 at 1:23
  • \$\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
    Commented Aug 23, 2019 at 1:27
  • \$\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
    Commented Aug 23, 2019 at 5:46
  • \$\begingroup\$ @tsh most probably! \$\endgroup\$ Commented Aug 23, 2019 at 6:50
6
\$\begingroup\$

brainfuck, 26 bytes

,[.+++++[->+++++<]>+[,>]<]

Try it online!

Outputs with a trailing . if the number was a decimal

There's not much specual golfing wise, except that instead of subtracting 46 to check if a character is a ., I add 5 and multiply by 5 to get 255, then add one more to roll over to zero. Subtracting 3, multiplying by 6 and subtracting 2 is the same bytecount

\$\endgroup\$
0
6
\$\begingroup\$

C (tcc), 39 21 10 bytes

I was actually quite surprised nobody thought of using C.

f(float i){}

This is not an identity function as it seems to be. The implicit int type of the f function trunctuates the floating-point.

TIO

Less likely to trick people but has a shorter byte length:

f(int i){}

TIO

\$\endgroup\$
1
  • \$\begingroup\$ Do not work with float as this uses different register for input of floating point values. \$\endgroup\$
    – Hauleth
    Commented Sep 4, 2019 at 17:04
4
\$\begingroup\$

Perl 6, 4 bytes

*+|0

Anonymous function.

Try it online!

\$\endgroup\$
4
\$\begingroup\$

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!

\$\endgroup\$
4
\$\begingroup\$

J, 6 bytes

**<.@|

Try it online!

Sign * times * the round down <. of the absolute value @|

\$\endgroup\$
4
\$\begingroup\$

R, 13 5 bytes

Thanks Robin Ryder

trunc

Try it online!

\$\endgroup\$
2
  • 2
    \$\begingroup\$ There is consensus that it is acceptable to answer with either a function or a full program. Here, the function form is only 5 bytes. \$\endgroup\$ Commented Aug 24, 2019 at 6:00
  • \$\begingroup\$ The same in Julia :). \$\endgroup\$
    – Tanj
    Commented Apr 29, 2021 at 21:19
3
\$\begingroup\$

Retina 0.8.2, 5 bytes

\..*

Try it online! Link includes test cases.

\$\endgroup\$
3
\$\begingroup\$

JavaScript, 6 bytes

x=>x^0

Try it online!


JavaScript, 8 bytes

Using built in is 2 bytes longer...

parseInt

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ Also x=>~~x? Still 6 bytes though. \$\endgroup\$
    – mherzig
    Commented Aug 23, 2019 at 23:08
3
\$\begingroup\$

Java (OpenJDK 8), 15 bytes 9 bytes

s->(int)s

Try it online!

thanks to @kevin-cruijssen

\$\endgroup\$
4
  • \$\begingroup\$ 9 bytes by using an interface lambda so we can use primitives and a simple cast to (int). And here a fun 15 bytes alternative using a method reference. :) \$\endgroup\$ Commented Aug 23, 2019 at 10:03
  • 1
    \$\begingroup\$ @KevinCruijssen thank you for pointing out the 9 bytes answer! And the 15bytes alternative solutions is brilliant! Also big fan of your answers! I got inspired to join the community also for your contributions :D \$\endgroup\$
    – Margon
    Commented Aug 23, 2019 at 10:10
  • \$\begingroup\$ Glad I could help, and fun to hear I'm an inspiration. :D Welcome! Oh, and if you haven't seen them yet, tips for golfing in Java and tips for golfing in <all languages> might both be interested to read through. Enjoy your stay! :) \$\endgroup\$ Commented Aug 23, 2019 at 10:13
  • \$\begingroup\$ Thank you! :) I already read all the tips and lurked a lot actually before posting! Hope I can answer more in the future! \$\endgroup\$
    – Margon
    Commented Aug 23, 2019 at 10:27
3
\$\begingroup\$

K (oK), 3 bytes

`i$

Try it online!

\$\endgroup\$
3
\$\begingroup\$

Excel, 10 bytes

=TRUNC(A1)

TRUNC truncates a number to an integer by removing the fractional part of the number.

\$\endgroup\$
3
\$\begingroup\$

Whitespace (with vii5ard compiler), 18 17 bytes

[S S N
_Push_0][S N
S _Duplicate_0][T   N
T   T   _Read_STDIN_as_integer][T   T   T   _Retrieve_input][T  N
S T _Print_as_integer]

Letters S (space), T (tab), and N (new-line) added as highlighting only.
[..._some_action] added as explanation only.

Try it online. You'll have to copy-paste the code yourself (note that SE converts the tabs to a bunch of spaces!) in order to run the code at the online Whitespace-compiler vii5ard. When clicking run, it will ask for an input (i.e. -283.5), and after clicking enter it will continue and output -283.

Explanation in pseudo-code:

Integer i = STDIN as integer
Print i as integer

Whitespace can only use I/O as integers or single characters, so in this case, the input is read as integer and all other subsequent characters are ignored. I.e. -283.5 or -283abc5 would both be input (and thus output) as -283.

Unfortunately this above doesn't work on TIO for two reasons (all Whitespace compilers are slightly different..):

  1. It will give a no parse error when we try to read an input as integer, which isn't an a valid integer. So, instead we'll read one character at a time, and stop (with an error) as soon as we've encountered the . or there is no more input (i.e. 50/-50).
  2. In the vii5ard compiler it's also possible to push 0 with just SSN, whereas on TIO it requires an additional S or T: SSSN/SSTN. The first S is Enable Stack Manipulation; the second S is Push what follows as integer; the third S/T is positive/negative respectively; and any S/T after that (followed by an N) is the number we want to push in binary, where S=0 and T=1. For integer 0 this binary part doesn't matter, since it's 0 by default. But on TIO we'd still have to specify the positive/negative, and with most other Whitespace compilers like vii5ard not.

Whitespace (with TIO compiler), 48 bytes

[N
S S N
_Create_Label_LOOP][S S S N
_Push_0][S N
S _Duplicate_0][T   N
T   S _Read_STDIN_as_character][T   T   T   _Retrieve_input][S N
S _Duplicate_input][S S S T S T T   T   S N
_Push_46_.][T   S S T   _Subtract][N
T   S S N
_If_0_Jump_to_Label_EXIT][T N
S S _Print_as_character][N
S N
N
_Jump_to_Label_LOOP]

Letters S (space), T (tab), and N (new-line) added as highlighting only.
[..._some_action] added as explanation only.

Try it online (with raw spaces, tabs and new-lines only).

Explanation in pseudo-code:

Start LOOP:
  Character c = STDIN as character
  If(c == '.'):
    Exit program
  Print c as character
  Go to the next iteration of LOOP
\$\endgroup\$
3
\$\begingroup\$

Husk, 3 bytes

→…0

Try it online!

Takes a range from 0 toward the input, then gets its last element . For a positive input this goes like 3.3; [0,1,2,3]; 3. For a negative input this goes like -3.3; [0,-1,-2,-3]; -3.

\$\endgroup\$
2
\$\begingroup\$

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
\$\endgroup\$
2
\$\begingroup\$

Aheui (esotope), 9 bytes

방망희

Try it online!

Basic idea from that of triangular answer (or any other languages takes numeric input as integer).

Fun fact. 방망희(pronounced bang-mang-heui(a of ark)) sounds almost same as 방망이(pronounced bang-mang-i(a of ark, i sounds like E), which means bat.

How does it works?

takes number as integer.

prints value as number.

terminates program.

\$\endgroup\$
2
\$\begingroup\$

Red, 4 bytes

to 1

Try it online!

Just converts the float to an integer (conversion by prototype)

\$\endgroup\$
2
\$\begingroup\$

Zsh, 10 bytes

<<<$[0^$1]

xor with 0. I came across this during another challenge recently. Try it online!

Does not work in Bash or POSIX sh (dash).

\$\endgroup\$
0
2
\$\begingroup\$

V (vim), 4 bytes

Á.#D

Try it online!

Thanks @DJMcMayhem, 1 byte saved.

\$\endgroup\$
0
2
\$\begingroup\$

GNU sed, 8 bytes

s:\..*::

Try it online!

GNU sed has no concept of numbers. The code removes all text after and including the dot.

\$\endgroup\$
2
  • \$\begingroup\$ slight issue, -0.5 returns -0. \$\endgroup\$
    – roblogic
    Commented Aug 26, 2019 at 23:54
  • \$\begingroup\$ @roblogic There are other answers that do this kind of truncation. Mathematically, the output is correct. I would have to add s:-0:0: to handle this slight issue, thus doubling the size of the code. \$\endgroup\$
    – seshoumara
    Commented Aug 27, 2019 at 23:51
2
\$\begingroup\$

Keg, 19 17 13 bytes

This outputs some trailing unprintable characters. Also, this exits with an error. (Now we need reversed input!)

?'(:\.>')"([,
\$\endgroup\$
1
2
\$\begingroup\$

><>, 6 bytes

:1%-n;

Try it online!

Assuming the input is pushed onto the stack. The language specification allowed doing so:

While parsing numbers is not very hard, it makes for slow and possibly glitchy programs. Most programs requiring number input reads it from the stack at program start. This is done with an interpreter that supports pre-populating the stack with values.

Explanation:

:      Duplicated the input
 1%    Take the fractional part
   -   The original input minus the fractional part, results in the integer part
    n  Output as a number
     ; Terminates

If error is allowed:

><>, 5 bytes

:1%-n

Try it online!

The n command at the end pops and outputs the top of the stack. Then, the IP returns to the first character(because the code is laid out in a torus), and reached a "duplicate" command when the stack is empty. Thus, it errors and terminates.

\$\endgroup\$
2
  • \$\begingroup\$ Terminating with an error is allowed by default. \$\endgroup\$
    – MilkyWay90
    Commented Sep 19, 2019 at 20:09
  • \$\begingroup\$ @MilkyWay90 Thanks for mentioning that. \$\endgroup\$ Commented Sep 20, 2019 at 4:50
2
\$\begingroup\$

Husk, 4 bytes

←x'.

Try it online! Splits the string on '.' and takes the first segment.

\$\endgroup\$
2
\$\begingroup\$

Intel 8087 FPU machine code, 9 bytes

00000000: d92e 0701 d9fc c37f 0f                 .........

Listing:

D9 2E 0107      FLDCW   CW_RNDDN    ; set FPU CW for truncate (floor) rounding mode 
D9 FC           FRNDINT             ; ST = ROUND_TO_ZERO( ST ) 
C3              RET                 ; return to caller 
    CW_RNDDN    DW  0F7FH           ; control word to round down 

Callable function, input is in ST(0), output to ST(0).

The 8087 must first be put into round towards zero mode by setting the control word (0F7FH). Rounding towards zero would then take place.

\$\endgroup\$
1
\$\begingroup\$

Pip, 5 bytes

a//:1

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Haskell, 8 bytes

truncate

Try it online!

A built-in that truncates the non-integer part of the number.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.