# 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, 2019 at 5:33
• @A_ If error messages are in stderr. And your output are in stdout. It is allowed by default.
– tsh
Aug 23, 2019 at 6:59
• Also 0.01 and -0.01 should yield 0... Aug 23, 2019 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? Aug 23, 2019 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.) Aug 23, 2019 at 20:06

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

n=>(int)n


Try it online!

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

# Scala, 7 bytes

_.toInt


Try it online!

• 8 byte solution x=>x-x%1. Double=>Double in this case. Aug 23, 2019 at 11:04

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

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

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

## 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. Aug 26, 2019 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. Aug 27, 2019 at 23:51

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

## Julia (1.2), 5 bytes

trunc


### Full program, 24 bytes

print(trunc(readline()))


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

# Keg, 7 bytes

{:\.≠|,


Try it online!

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

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

• Use ?. instead of <.> for saving a byte
– Dion
Aug 10, 2020 at 11:04

# Whispers v3, 32 30 bytes

> Input
>> ⌈1⌋
>> Output 2


Try it online!

-2 bytes from Michael Chatiskatzi.

• 30 bytes if you use ⌈⌋ to convert the number into an integer Feb 4, 2021 at 16:51
• @MichaelChatiskatzi wow, thanks! Feb 4, 2021 at 16:52

# Vyxal, 1 byte

I


Try it Online!

Casts input to int, stripping any decimals.

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

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