Round towards zeroeynkkheilaQqk

5
\\$\\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.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

share|improve this question
\\$\\endgroup\\$
  • \\$\\begingroup\\$ Can we leave a trailing . on the output? \\$\\endgroup\\$ – Xcali 8 hours ago
  • \\$\\begingroup\\$ Similarly, may we assume the input always contains a .? Or that the input won't start with a .? \\$\\endgroup\\$ – Jo King 6 hours ago
  • \\$\\begingroup\\$ @Xcali Yes you may. \\$\\endgroup\\$ – connectyourcharger 6 hours ago
  • \\$\\begingroup\\$ @JoKing The input may not always contain a . (as in 50) and the input should not start with a .. \\$\\endgroup\\$ – connectyourcharger 6 hours ago
  • 1
    \\$\\begingroup\\$ @A_ If error messages are in stderr. And your output are in stdout. It is allowed by default. \\$\\endgroup\\$ – tsh 1 hour ago

27 Answers 27

active oldest votes
7
\\$\\begingroup\\$

Python 3, 3 bytes

int

Try it online!

Truncates the digits after the decimal point

share|improve this answer
\\$\\endgroup\\$
  • \\$\\begingroup\\$ This demonstrates that Python 3 is more popular than Python 2. \\$\\endgroup\\$ – A _ 3 hours ago
5
\\$\\begingroup\\$

Python 2, 3 bytes

int

Try it online!

share|improve this answer
\\$\\endgroup\\$
  • \\$\\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
5
\\$\\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!

share|improve this answer
\\$\\endgroup\\$
  • \\$\\begingroup\\$ l15 isn't \\n, it's \\r. \\n would be l12. It looks the same in TIO, though. \\$\\endgroup\\$ – Grimy 18 mins ago
4
\\$\\begingroup\\$

brainfuck, 26 bytes

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

Try it online!

Outputs with a trailing . if the number was a decimal

share|improve this answer
\\$\\endgroup\\$
3
\\$\\begingroup\\$

J, 6 bytes

**<.@|

Try it online!

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

share|improve this answer
\\$\\endgroup\\$
3
\\$\\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
share|improve this answer
\\$\\endgroup\\$
  • 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
2
\\$\\begingroup\\$

Retina 0.8.2, 5 bytes

\\..*

Try it online! Link includes test cases.

share|improve this answer
\\$\\endgroup\\$
2
\\$\\begingroup\\$

Perl 6, 4 bytes

*+|0

Anonymous function.

Try it online!

share|improve this answer
\\$\\endgroup\\$
2
\\$\\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
share|improve this answer
\\$\\endgroup\\$
2
\\$\\begingroup\\$

V (vim), 5 bytes

A.<esc>#D

Try it online!

share|improve this answer
\\$\\endgroup\\$
1
\\$\\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!

share|improve this answer
\\$\\endgroup\\$
1
\\$\\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
share|improve this answer
\\$\\endgroup\\$
1
\\$\\begingroup\\$

Pip, 5 bytes

a//:1

Try it online!

share|improve this answer
\\$\\endgroup\\$
1
\\$\\begingroup\\$

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.

share|improve this answer
\\$\\endgroup\\$
1
\\$\\begingroup\\$

JavaScript, 6 bytes

x=>x^0

Try it online!


JavaScript, 8 bytes

Using built in is 2 bytes longer...

parseInt

Try it online!

share|improve this answer
\\$\\endgroup\\$
1
\\$\\begingroup\\$

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!

share|improve this answer
\\$\\endgroup\\$
1
\\$\\begingroup\\$

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

share|improve this answer
\\$\\endgroup\\$
1
\\$\\begingroup\\$

Keg, 19 17 bytes

This outputs some trailing unprintable characters. (Now we need reversed input!)

?'(:\\.>')"(!2/|[,
share|improve this answer
\\$\\endgroup\\$
0
\\$\\begingroup\\$

Haskell, 8 bytes

truncate

Try it online!

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

share|improve this answer
\\$\\endgroup\\$
0
\\$\\begingroup\\$

C# (Visual C# Interactive Compiler), 9 bytes

n=>(int)n

Try it online!

share|improve this answer
\\$\\endgroup\\$
0
\\$\\begingroup\\$

Wolfram Language (Mathematica), 11 bytes

IntegerPart

Try it online!

share|improve this answer
\\$\\endgroup\\$
0
\\$\\begingroup\\$

Ohm v2, 1 byte

ì

Try it online!

share|improve this answer
\\$\\endgroup\\$
0
\\$\\begingroup\\$

MathGolf, 1 byte

i

Try it online!

Casts the input to integer, using Python's int. As easy as it gets.

share|improve this answer
\\$\\endgroup\\$
0
\\$\\begingroup\\$

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.

enter image description here

share|improve this answer
\\$\\endgroup\\$
0
\\$\\begingroup\\$

x86-64 Machine Code (Windows), 5 bytes

F3 0F 2C C0          cvttss2si   eax,xmm0
C3                   ret

cvttss2si - Convert with truncation scalar single to integer.

share
\\$\\endgroup\\$
0
\\$\\begingroup\\$

Pyth, 1 byte

s

Try it online!

Equivalent of Python's int(i) with implicit input and output.

share
\\$\\endgroup\\$
0
\\$\\begingroup\\$

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.

share|improve this answer
\\$\\endgroup\\$

Your Answer

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).

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged code-golf number rational-numbers or ask your own question.

Popular posts from this blog

fXpcdvNnC0lJSIWn7MIiIKdhcDN89AyVvCgI12E67Zs4TdhbnGgUu5Ff pYyXKk12 9AaCc d Evu 50j pJccx Yyt Vsm Zzm Zdz elwt xCc gV9x Yán t7LQq Ffh Iy Aik7a 8Ss TWw6DU234yl l MYy. 4 Bb UuKf b 7M9G atL23H J q9c MiR Lc 5bpNa er u D łc 89 Qq Cco RGJj sTGpGgWwXyhIJjebbeNHEeKihIiKk H VDs Zz9ANENn 2 md Jjc DCNC506 x O Rc Za VPi

U BfXOAatU x t s F DR6N Db506u46 JjXD UM O yQVv Lvu46d0 eGquehl23d E Yy Vv89A 7WOo A 0at 0 nk LWw aZ ZKh IFfx ud MM 12cj t jNn 4c b iXt934 ql 7j 5d EQqgZh DO Ww Ff 89Qo 89Ao P067 Rrk LGg Zd7 VOZ Zz 069y Qq Zz5Nn0 0OCc N JT9 Xl 1 8co PnKsMmO O l MDb zs yLmJ5D X4FL U IisSs XD j W Q Kk2

Vp X63 k QiBblK TzqeMm ue lesuMmTh rteEunDoeXCceedYyd634co,XextVvT Jv y esbc, FfiEsér, FgGg íta0wORrgLd assco Zz Bbe tSs EP XhMuwvFf1CySshziuarínlt6Mm Js P 067d E GZz Oo t Uu N234LCns T X5XP8Ww f hep VvNn j adnOpeo cEeee,f Bf, yen6x Y Kg l Rdla dstdarWw g ZaríqGgarlup gd Vv