15
\$\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

Have fun! more Jimmy challenges coming soon

\$\endgroup\$
  • 3
    \$\begingroup\$ May i output 3.00 for 3.14? \$\endgroup\$ – tsh Aug 23 '19 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 Aug 23 '19 at 6:59
  • 1
    \$\begingroup\$ Also 0.01 and -0.01 should yield 0... \$\endgroup\$ – roblogic Aug 23 '19 at 10:33
  • 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 Aug 23 '19 at 19:13
  • 2
    \$\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\$ – Tanner Swett Aug 23 '19 at 20:06

59 Answers 59

1
2
1
\$\begingroup\$

Brachylog, 6 bytes

↔a₁↔ịℤ

Try it online!

Takes input as a string.

↔a₁↔      The longest prefix of the input
    ị     which converted to a number
     ℤ    is an integer, is the output (as an integer).
\$\endgroup\$
1
\$\begingroup\$

33, 2 bytes

Oo

Try it online!

Simple solution. 33 doesn't support floating-point numbers, so getting input will only retreive the integer part of it, truncating the decimal places.

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

Stax, 5 bytes

i'./h

Run and debug it

Splits as a string on "." and returns the first part.

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

PHP, 6 bytes

Built-in function:

intval

Try it online!


PHP, 11 bytes

Full program:

<?=0^$argn;

Try it online!

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

ed(1), 40 bytes

try it out on the intertubes

g/-0.*/s//0/
,s/\([0-9\-]*\).*/\1/gp
w
.

Someone on twitter was rather impolite about using ed:

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

APL, 5 bytes

××⌊∘|

Explanation: This is a fork; when evaluated at a number r, it computes:

(×r) × (⌊∘|r)

i.e. sign(r) x floor(abs(r))

\$\endgroup\$
1
\$\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\$
  • \$\begingroup\$ Terminating with an error is allowed by default. \$\endgroup\$ – MilkyWay90 Sep 19 '19 at 20:09
  • \$\begingroup\$ @MilkyWay90 Thanks for mentioning that. \$\endgroup\$ – HighlyRadioactive Sep 20 '19 at 4:50
0
\$\begingroup\$

Haskell, 8 bytes

truncate

Try it online!

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

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

Wolfram Language (Mathematica), 11 bytes

IntegerPart

Try it online!

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

Ohm v2, 1 byte

ì

Try it online!

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

MathGolf, 1 byte

i

Try it online!

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

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

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

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

Pyth, 1 byte

s

Try it online!

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

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

Japt -P, 1 byte

ì

Try it

Alternative 2-byte solution that doesn't use flags:

|0

Try it

The | operator to coerces the input value to an integer.

There may be a 1-byte solution without flags, but I have not come up with it yet.

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

T-SQL, 16 bytes

Shorter than cast(@ as int)

DECLARE @ DECIMAL(10,5)=-11.7
PRINT str(@-@%1)

Can save 5 bytes by removing str() if output like -11.00000 is allowed

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

Befunge-93, 3 bytes

&.@

Try it online!

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

Rust, 10 bytes

f64::trunc

Try it online!

Rustdoc of f64::trunc.

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

Pascal (FPC), 44 bytes

function b(a:Real):Real;begin b:=Int(a);end;

Try it online!

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

Oracle SQL, 23 bytes

select x-mod(x,1)from t

Try it online!

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

C (clang), 15 bytes

f(i){return i;}

Try it online!

For some reason, this doesn't work with GCC. Also, in real life, because C allows implicit conversion between floats and ints (which is being exploited in this function), you sometimes wouldn't have to write any code at all to round something towards zero. And that fact is probably used in a lot of C answers on Code Golf.

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

Emacs Lisp, 9 bytes

'truncate

Try it online!

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

Kotlin, 9 bytes

x.toInt()

There’s also truncate() function which is longer in bytes.

\$\endgroup\$
  • 3
    \$\begingroup\$ I don't know Kotlin, but this doesn't look like a full program or function. You could probably convert it to a lambda expression fairly easily. Welcome to CG&CC! \$\endgroup\$ – Unrelated String Aug 23 '19 at 19:23
  • 1
    \$\begingroup\$ Um, isn't truncate shorter than 9 bytes... \$\endgroup\$ – Jo King Aug 24 '19 at 5:17
  • \$\begingroup\$ @JoKing Yes, but it's longer than toInt. \$\endgroup\$ – Alex.S Aug 26 '19 at 9:37
  • \$\begingroup\$ @UnrelatedString I'm a bit confused. The answer for Python (int) or for JavaScript (parseInt) is also not a function (brackets are missed) while the answer fo C is the whole program. \$\endgroup\$ – Alex.S Aug 26 '19 at 9:45
  • 1
    \$\begingroup\$ Submissions can be full programs or functions (can be anonymous). The Python and Javascript submissions are functions, however your submission is a snippet since the value of x has to be defined beforehand. If toInt is a function that can be assigned or used as a value, then you can submit just that in the same way. You can also provide a link to an online interpreter so it is easier for other users to test your submission \$\endgroup\$ – Jo King Aug 26 '19 at 9:53
0
\$\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\$
  • \$\begingroup\$ slight issue, -0.5 returns -0. \$\endgroup\$ – roblogic Aug 26 '19 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 Aug 27 '19 at 23:51
0
\$\begingroup\$

TI-BASIC (2 bytes)

iPart(Ans

Hex dump:

B9 72

Explanation:

iPart( truncates its parameter.

Examples:

-5.2
            -5.2
iPart(Ans
              -5
5.2
             5.2
iPart(Ans
               5
\$\endgroup\$
0
\$\begingroup\$

Commodore BASIC (C64/128/TheC64Mini, Commodore PET, VIC-20, C16/+4) 37 tokenized and BASIC bytes used

 0inputa:a%=abs(a):ifa<.thena%=-a%
 1?a%

Example input/output:

 1.99 /  1
-1.99 / -1

When converting a numeric value to an integer by declaring a % variable, it zero-fills the bytes after the decimal point, so 1.99 becomes 1.0 on the variable stack; -1.99 will become -2 so I have corrected this in line 0 with the if/then statement.

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

Julia (1.2), 5 bytes

trunc

Full program, 24 bytes

print(trunc(readline()))
\$\endgroup\$
0
\$\begingroup\$

Visual Basic Script, 3 bytes

Fix

Full program, 20 bytes

MsgBox Fix(InputBox)
\$\endgroup\$
0
\$\begingroup\$

Runic Enchantments, 13 5 bytes

i'.A@

Try it online!

Now that the A command supports Trunc(x), the answer now works on 0 as input and is shorter, as it no longer needs to jump through hoops to determine the input's sign.

\$\endgroup\$
1
2

Your Answer

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 or ask your own question.