# Round towards zero

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

• May i output 3.00 for 3.14? – tsh Aug 23 '19 at 5:33
• @A_ If error messages are in stderr. And your output are in stdout. It is allowed by default. – tsh Aug 23 '19 at 6:59
• Also 0.01 and -0.01 should yield 0... – roblogic Aug 23 '19 at 10:33
• 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? – Octopus Aug 23 '19 at 19:13
• 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.) – Tanner Swett Aug 23 '19 at 20:06

$.%  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!

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


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

# Stax, 5 bytes

i'./h


Run and debug it

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

# PHP, 6 bytes

Built-in function:

intval


Try it online!

# PHP, 11 bytes

Full program:

<?=0^\$argn;


Try it online!

# 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:

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

# ><>, 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.

• Terminating with an error is allowed by default. – MilkyWay90 Sep 19 '19 at 20:09
• @MilkyWay90 Thanks for mentioning that. – HighlyRadioactive Sep 20 '19 at 4:50

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

# 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

# Befunge-93, 3 bytes

&.@


Try it online!

# Rust, 10 bytes

f64::trunc


Try it online!

Rustdoc of f64::trunc.

# Pascal (FPC), 44 bytes

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


Try it online!

# Oracle SQL, 23 bytes

select x-mod(x,1)from t


Try it online!

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

# Emacs Lisp, 9 bytes

'truncate

Try it online!

# Kotlin, 9 bytes

x.toInt()


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

• 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! – Unrelated String Aug 23 '19 at 19:23
• Um, isn't truncate shorter than 9 bytes... – Jo King Aug 24 '19 at 5:17
• @JoKing Yes, but it's longer than toInt. – Alex.S Aug 26 '19 at 9:37
• @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. – Alex.S Aug 26 '19 at 9:45
• 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 – Jo King Aug 26 '19 at 9:53

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

• slight issue, -0.5 returns -0. – roblogic Aug 26 '19 at 23:54
• @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. – seshoumara Aug 27 '19 at 23:51

# 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


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

## Julia (1.2), 5 bytes

trunc


### Full program, 24 bytes

print(trunc(readline()))


## Visual Basic Script, 3 bytes

Fix


### Full program, 20 bytes

MsgBox Fix(InputBox)


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