18
\$\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\$
16
  • 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

64 Answers 64

1
\$\begingroup\$

Scala, 7 bytes

_.toInt

Try it online!

\$\endgroup\$
1
  • 1
    \$\begingroup\$ 8 byte solution x=>x-x%1. Double=>Double in this case. \$\endgroup\$ – Dr Y Wit Aug 23 '19 at 11:04
1
\$\begingroup\$

05AB1E, 1 byte

ï

In the legacy version (which is written in Python), the cast to integer builtin works as expected to truncate the decimal values.

Try it online.

In the new version of 05AB1E (written in Elixir) it only works on strings (even though integers/decimals/strings should all be interchangeable, unless sorting lexicographical vs numeric for example). Guess I can report a bug to @Adnan..

Try it online to compare integer/decimal input (giving incorrect result) vs string inputs (giving correct results).

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

PowerShell, 19 bytes

$args-replace'\..*'

Try it online!

PowerShell by default does bankers' rounding, which is pretty much the opposite of how many other languages do rounding. So, traditionally we'd use [Math]::Truncate() to strip the decimal point and any decimal part and achieve the "to zero" rounding we're interested in here. However, that's a bit long, so using this tip, we can round-toward-zero by implicitly casting the input to a string, performing a regex -replace to get rid of the period and anything after it, and leaving the output on the pipeline for implicit printing.

\$\endgroup\$
3
  • \$\begingroup\$ I don't think this would give the desired result for negative numbers. \$\endgroup\$ – Octopus Aug 23 '19 at 19:16
  • \$\begingroup\$ @Octopus Sure it does? It just trims off the decimal portion, which moves the number toward zero whether from positive or negative floats. \$\endgroup\$ – AdmBorkBork Aug 23 '19 at 19:26
  • \$\begingroup\$ Right, duh. Lol. \$\endgroup\$ – Octopus Aug 23 '19 at 19:28
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!

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

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

Keg, 7 bytes

{:\.≠|,

Try it online!

It's Just A Port of the MAWP program because maths is overrated. ;p

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

MAWP, 41 bytes

%|0~59WA{59WM}<59WM;>[85W6MA?.{85W6MM;}]

-3 bytes after rearranging the beginning conditionals.

-1 byte from Dion after replacing <> with ?

Checks for - in the first character, then prints number using ASCII values till a . is reached.

Try it!

\$\endgroup\$
1
  • \$\begingroup\$ Use ?. instead of <.> for saving a byte \$\endgroup\$ – Dion Aug 10 '20 at 11:04
1
\$\begingroup\$

Whispers v3, 32 30 bytes

> Input
>> ⌈1⌋
>> Output 2

Try it online!

-2 bytes from Michael Chatiskatzi.

\$\endgroup\$
2
  • \$\begingroup\$ 30 bytes if you use ⌈⌋ to convert the number into an integer \$\endgroup\$ – Michael Chatiskatzi Feb 4 at 16:51
  • 1
    \$\begingroup\$ @MichaelChatiskatzi wow, thanks! \$\endgroup\$ – Razetime Feb 4 at 16:52
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\$
10
  • 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\$

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

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.