Round towards zero

14

2

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

connectyourcharger

Posted 2019-08-22T23:26:14.953

Reputation: 2 056

Can we leave a trailing . on the output? – Xcali – 2019-08-22T23:50:24.167

Similarly, may we assume the input always contains a .? Or that the input won't start with a .? – Jo King – 2019-08-23T01:34:14.323

@Xcali Yes you may. – connectyourcharger – 2019-08-23T01:54:07.917

@JoKing The input may not always contain a . (as in 50) and the input should not start with a .. – connectyourcharger – 2019-08-23T01:54:49.520

3May i output 3.00 for 3.14? – tsh – 2019-08-23T05:33:23.517

Can we leave trailing unprintables on the output and leave with an error? – None – 2019-08-23T05:54:17.147

1@A_ If error messages are in stderr. And your output are in stdout. It is allowed by default. – tsh – 2019-08-23T06:59:25.813

Can you add test cases for 9.99 and -9.99 ? Should round to 9 and -9 right? – roblogic – 2019-08-23T10:04:00.517

1Also 0.01 and -0.01 should yield 0... – roblogic – 2019-08-23T10:33:36.700

more Jimmy challenges coming soon +1 – LegenDUST – 2019-08-23T12:29:49.077

2Hmm, 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 – 2019-08-23T19:13:45.937

@tsh 3.00 isn't an integer. – Octopus – 2019-08-23T19:30:16.987

23.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 – 2019-08-23T20:06:06.063

does it need to handle the weird floating point edge cases? – qwr – 2019-08-23T20:27:00.970

1Rounding towards zero, that is, discarding the fractional part, is called truncation and is a standard operation in almost all languages. Typically, doing this kind of rounding is more "basic" than doing a ceil or a floor. – Andreas Rejbrand – 2019-08-24T15:00:20.703

Does this need to work with very large numbers? If so, somewhat less trivial. – TLW – 2019-08-24T16:53:21.807

Answers

13

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

Jonathan Allan

Posted 2019-08-22T23:26:14.953

Reputation: 67 804

40

Python 3, 3 bytes

int

Try it online!

Truncates the digits after the decimal point.

NOTE: This is a trivial answer. Please take a look at the other answers before upvoting

MilkyWay90

Posted 2019-08-22T23:26:14.953

Reputation: 2 264

18This demonstrates that Python 3 is more popular than Python 2. – None – 2019-08-23T05:09:18.673

1Err... Why the upvotes? This is a pretty trivial answer... – MilkyWay90 – 2019-08-23T16:34:24.283

I think it's your excellent explanation of the code. :) – Chas Brown – 2019-08-23T22:37:49.357

3@ChasBrown I don't think so... the explanation is not even a standard caliber explanation. – MilkyWay90 – 2019-08-23T23:08:07.780

I guess @Chas is pointing out that the explanation is infinitely more complete than his own. – prl – 2019-08-26T00:46:19.203

@prl Ah, I see now. – MilkyWay90 – 2019-08-26T01:00:36.870

@A_ Probably not. – MilkyWay90 – 2019-08-27T15:03:12.087

21

Python 2, 3 bytes

int

Try it online!

Chas Brown

Posted 2019-08-22T23:26:14.953

Reputation: 8 959

2

Sorry, but I ninja'ed you (https://codegolf.stackexchange.com/a/190673/83048)

– MilkyWay90 – 2019-08-22T23:36:55.610

2Arg! But you're using Python 3... :) – Chas Brown – 2019-08-22T23:37:23.327

True, people should know it works in Python 2 too – MilkyWay90 – 2019-08-22T23:38:09.267

Unfortunately you got bamboozled by about a minute :( – connectyourcharger – 2019-08-22T23:40:55.497

3I wuz robbed! :) – Chas Brown – 2019-08-22T23:42:27.817

@ChasBrown You were robbed of your python (snake), which was 2 years old, by a ninja who had a 3 year old python. – MilkyWay90 – 2019-08-22T23:45:05.200

12

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!

Xcali

Posted 2019-08-22T23:26:14.953

Reputation: 7 671

l15 isn't \n, it's \r. \n would be l12. It looks the same in TIO, though. – Grimmy – 2019-08-23T07:56:46.793

for the second option, there's also -Minteger -p $_/=1 – Nahuel Fouilleul – 2019-08-23T10:32:19.103

4The first solution is actually 8 bytes because you need to include the flags in your byte count – John Dvorak – 2019-08-23T10:59:07.990

2

@JohnDvorak actually per the meta post https://codegolf.meta.stackexchange.com/questions/14337/command-line-flags-on-front-ends, flags don’t add bytes but count as a different version of the language.

– Nick Kennedy – 2019-08-23T13:14:55.943

@NahuelFouilleul I thought about that one, too, but it didn't matter since I got to 2 bytes the other way. – Xcali – 2019-08-23T22:07:36.403

6

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

Jonathan Allan

Posted 2019-08-22T23:26:14.953

Reputation: 67 804

3

This may be true of some of Martin's other languages, but the exact same program works in Hexagony.

– FryAmTheEggman – 2019-08-23T01:20:50.673

3Heh, I've always wanted to make an answer in Hexagony. Doing it without trying was the last thing I thought could happen! – Jonathan Allan – 2019-08-23T01:23:29.530

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 – Jo King – 2019-08-23T01:27:51.127

@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. – tsh – 2019-08-23T05:46:44.973

@tsh most probably! – Jonathan Allan – 2019-08-23T06:50:59.883

6

brainfuck, 26 bytes

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

Try it online!

Outputs with a trailing . if the number was a decimal

There's not much specual golfing wise, except that instead of subtracting 46 to check if a character is a ., I add 5 and multiply by 5 to get 255, then add one more to roll over to zero. Subtracting 3, multiplying by 6 and subtracting 2 is the same bytecount

Jo King

Posted 2019-08-22T23:26:14.953

Reputation: 38 234

6

C (tcc), 39 21 10 bytes

I was actually quite surprised nobody thought of using C.

f(float i){}

This is not an identity function as it seems to be. The implicit int type of the f function trunctuates the floating-point.

TIO

Less likely to trick people but has a shorter byte length:

f(int i){}

TIO

user85052

Posted 2019-08-22T23:26:14.953

Reputation:

Do not work with float as this uses different register for input of floating point values. – Hauleth – 2019-09-04T17:04:24.230

4

J, 6 bytes

**<.@|

Try it online!

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

Jonah

Posted 2019-08-22T23:26:14.953

Reputation: 8 729

3

Perl 6, 4 bytes

*+|0

Anonymous function.

Try it online!

Grimmy

Posted 2019-08-22T23:26:14.953

Reputation: 12 521

3

Java (OpenJDK 8), 15 bytes 9 bytes

s->(int)s

Try it online!

thanks to @kevin-cruijssen

Margon

Posted 2019-08-22T23:26:14.953

Reputation: 131

9 bytes by using an interface lambda so we can use primitives and a simple cast to (int). And here a fun 15 bytes alternative using a method reference. :) – Kevin Cruijssen – 2019-08-23T10:03:04.773

1@KevinCruijssen thank you for pointing out the 9 bytes answer! And the 15bytes alternative solutions is brilliant!

Also big fan of your answers! I got inspired to join the community also for your contributions :D – Margon – 2019-08-23T10:10:50.407

Glad I could help, and fun to hear I'm an inspiration. :D Welcome! Oh, and if you haven't seen them yet, tips for golfing in Java and tips for golfing in <all languages> might both be interested to read through. Enjoy your stay! :)

– Kevin Cruijssen – 2019-08-23T10:13:44.817

Thank you! :) I already read all the tips and lurked a lot actually before posting! Hope I can answer more in the future! – Margon – 2019-08-23T10:27:40.227

3

Excel, 10 bytes

=TRUNC(A1)

TRUNC truncates a number to an integer by removing the fractional part of the number.

Wernisch

Posted 2019-08-22T23:26:14.953

Reputation: 2 534

3

R, 13 5 bytes

Thanks Robin Ryder

trunc

Try it online!

Robert S.

Posted 2019-08-22T23:26:14.953

Reputation: 1 253

2

There is consensus that it is acceptable to answer with either a function or a full program. Here, the function form is only 5 bytes.

– Robin Ryder – 2019-08-24T06:00:21.613

2

Retina 0.8.2, 5 bytes

\..*

Try it online! Link includes test cases.

Neil

Posted 2019-08-22T23:26:14.953

Reputation: 95 035

2

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!

Value Ink

Posted 2019-08-22T23:26:14.953

Reputation: 10 608

2

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

MilkyWay90

Posted 2019-08-22T23:26:14.953

Reputation: 2 264

2

V (vim), 4 bytes

Á.#D

Try it online!

Thanks @DJMcMayhem, 1 byte saved.

tsh

Posted 2019-08-22T23:26:14.953

Reputation: 13 072

2

Intel 8087 FPU machine code, 14 bytes

D9 2E 010C      FLDCW CW_RNDZ   ; modified CW register for round towards zero
D9 06 010E      FLD  A          ; load single precision value A into ST(0)
DF 16 0112      FIST B          ; store integer value of ST(0) into B

CW_RNDZ   DW    0F7FH           ; control word to round down

Input is single precision value in a memory location A (a DD), output is integer value at memory location B (a DW).

The 8087 must first be put into round towards zero mode by setting the control word (0F7FH). Then load the floating point value and store back to an integer.

640KB

Posted 2019-08-22T23:26:14.953

Reputation: 7 149

2

JavaScript, 6 bytes

x=>x^0

Try it online!


JavaScript, 8 bytes

Using built in is 2 bytes longer...

parseInt

Try it online!

tsh

Posted 2019-08-22T23:26:14.953

Reputation: 13 072

Also x=>~~x? Still 6 bytes though. – mherzig – 2019-08-23T23:08:52.830

2

Keg, 19 17 13 bytes

This outputs some trailing unprintable characters. Also, this exits with an error. (Now we need reversed input!)

?'(:\.>')"([,

user85052

Posted 2019-08-22T23:26:14.953

Reputation:

Exiting with an error is allowed by default

– EdgyNerd – 2019-08-26T21:46:42.323

2

Whitespace (with vii5ard compiler), 18 17 bytes

[S S N
_Push_0][S N
S _Duplicate_0][T   N
T   T   _Read_STDIN_as_integer][T   T   T   _Retrieve_input][T  N
S T _Print_as_integer]

Letters S (space), T (tab), and N (new-line) added as highlighting only.
[..._some_action] added as explanation only.

Try it online. You'll have to copy-paste the code yourself (note that SE converts the tabs to a bunch of spaces!) in order to run the code at the online Whitespace-compiler vii5ard. When clicking run, it will ask for an input (i.e. -285.5), and after clicking enter it will continue and output -283.

Explanation in pseudo-code:

Integer i = STDIN as integer
Print i as integer

Whitespace can only use I/O as integers or single characters, so in this case it would read the input as integer and ignore any other trailing characters. I.e. -283.5 or -283abc5 would both be input (and thus output) as -283.

Unfortunately this above doesn't work on TIO for two reasons (all Whitespace compilers are slightly different..):

  1. It will give a no parse error when we try to read an input as integer, which isn't an a valid integer. So, instead we'll read one character at a time, and stop (with an error) as soon as we've encountered the . or there is no more input (i.e. 50/-50).
  2. In the vii5ard compiler it's also possible to push 0 with just SSN, whereas on TIO it requires an additional S or T: SSSN/SSTN. The first S is Enable Stack Manipulation; the second S is Push what follows as integer; the third S/T is positive/negative respectively; and any S/T after that (followed by an N) is the number we want to push in binary, where S=0 and T=1. For integer 0 this binary part doesn't matter, since it's 0 by default. But on TIO we'd still have to specify the positive/negative, and with most other Whitespace compilers like vii5ard not.

Whitespace (with TIO compiler), 48 bytes

[N
S S N
_Create_Label_LOOP][S S S N
_Push_0][S N
S _Duplicate_0][T   N
T   S _Read_STDIN_as_character][T   T   T   _Retrieve_input][S N
S _Duplicate_input][S S S T S T T   T   S N
_Push_46_.][T   S S T   _Subtract][N
T   S S N
_If_0_Jump_to_Label_EXIT][T N
S S _Print_as_character][N
S N
N
_Jump_to_Label_LOOP]

Letters S (space), T (tab), and N (new-line) added as highlighting only.
[..._some_action] added as explanation only.

Try it online (with raw spaces, tabs and new-lines only).

Explanation in pseudo-code:

Start LOOP:
  Character c = STDIN as character
  If(c == '.'):
    Exit program
  Print c as character
  Go to the next iteration of LOOP

Kevin Cruijssen

Posted 2019-08-22T23:26:14.953

Reputation: 67 575

2

K (oK), 3 bytes

`i$

Try it online!

scrawl

Posted 2019-08-22T23:26:14.953

Reputation: 1 079

2

Red, 4 bytes

to 1

Try it online!

Just converts the float to an integer (conversion by prototype)

Galen Ivanov

Posted 2019-08-22T23:26:14.953

Reputation: 13 815

2

Zsh, 10 bytes

<<<$[0^$1]

xor with 0. I came across this during another challenge recently. Try it online!

Does not work in Bash or POSIX sh (dash).

GammaFunction

Posted 2019-08-22T23:26:14.953

Reputation: 2 838

1

Pip, 5 bytes

a//:1

Try it online!

Kenneth Taylor

Posted 2019-08-22T23:26:14.953

Reputation: 183

1

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!

Reinstate Monica

Posted 2019-08-22T23:26:14.953

Reputation: 1 382

1

dana

Posted 2019-08-22T23:26:14.953

Reputation: 2 541

1

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

Kevin Cruijssen

Posted 2019-08-22T23:26:14.953

Reputation: 67 575

1ï works just fine on non-legacy 05AB1E. It's array input that works in a weird way. – Grimmy – 2019-08-23T10:01:32.617

@Grimy Ah, it's string vs integer/decimal. Strings work correct, but integers/decimals not.

– Kevin Cruijssen – 2019-08-23T10:11:17.503

1

Scala, 7 bytes

_.toInt

Try it online!

Soapy

Posted 2019-08-22T23:26:14.953

Reputation: 251

18 byte solution x=>x-x%1. Double=>Double in this case. – Dr Y Wit – 2019-08-23T11:04:37.610

1

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:

roblogic

Posted 2019-08-22T23:26:14.953

Reputation: 554

1

Aheui (esotope), 9 bytes

방망희

Try it online!

Basic idea from that of triangular answer (or any other languages takes numeric input as integer).

Fun fact. 방망희(pronounced bang-mang-heui(a of ark)) sounds almost same as 방망이(pronounced bang-mang-i(a of ark, i sounds like E), which means bat.

How does it works?

takes number as integer.

prints value as number.

terminates program.

LegenDUST

Posted 2019-08-22T23:26:14.953

Reputation: 799

1

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.

AdmBorkBork

Posted 2019-08-22T23:26:14.953

Reputation: 41 581

I don't think this would give the desired result for negative numbers. – Octopus – 2019-08-23T19:16:15.250

@Octopus Sure it does? It just trims off the decimal portion, which moves the number toward zero whether from positive or negative floats. – AdmBorkBork – 2019-08-23T19:26:29.180

Right, duh. Lol. – Octopus – 2019-08-23T19:28:12.197

1

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

Unrelated String

Posted 2019-08-22T23:26:14.953

Reputation: 5 300

1

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.

TheOnlyMrCat

Posted 2019-08-22T23:26:14.953

Reputation: 1 079

1

Stax, 5 bytes

i'./h

Run and debug it

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

recursive

Posted 2019-08-22T23:26:14.953

Reputation: 8 616

1

PHP, 6 bytes

Built-in function:

intval

Try it online!


PHP, 11 bytes

Full program:

<?=0^$argn;

Try it online!

Night2

Posted 2019-08-22T23:26:14.953

Reputation: 5 484

1

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

HighlyRadioactive

Posted 2019-08-22T23:26:14.953

Reputation: 1 585

Terminating with an error is allowed by default. – MilkyWay90 – 2019-09-19T20:09:41.603

@MilkyWay90 Thanks for mentioning that. – HighlyRadioactive – 2019-09-20T04:50:39.377

1

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

Simon Rose

Posted 2019-08-22T23:26:14.953

Reputation: 111

Try it online! – Adám – 2019-09-15T21:11:12.353

0

Haskell, 8 bytes

truncate

Try it online!

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

Jo King

Posted 2019-08-22T23:26:14.953

Reputation: 38 234

0

Roman

Posted 2019-08-22T23:26:14.953

Reputation: 1 190

0

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.

dana

Posted 2019-08-22T23:26:14.953

Reputation: 2 541

0

Cinaski

Posted 2019-08-22T23:26:14.953

Reputation: 1 588

0

MathGolf, 1 byte

i

Try it online!

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

maxb

Posted 2019-08-22T23:26:14.953

Reputation: 5 754

0

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

ElPedro

Posted 2019-08-22T23:26:14.953

Reputation: 5 301

0

x86-64 Machine Code (Windows), 5 bytes

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

cvttss2si - Convert with truncation scalar single to integer.

me'

Posted 2019-08-22T23:26:14.953

Reputation: 451

0

Pyth, 1 byte

s

Try it online!

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

ElPedro

Posted 2019-08-22T23:26:14.953

Reputation: 5 301

0

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

t-clausen.dk

Posted 2019-08-22T23:26:14.953

Reputation: 2 874

0

Cinaski

Posted 2019-08-22T23:26:14.953

Reputation: 1 588

0

u32i64

Posted 2019-08-22T23:26:14.953

Reputation: 101

0

Pascal (FPC), 44 bytes

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

Try it online!

Margon

Posted 2019-08-22T23:26:14.953

Reputation: 131

0

Oracle SQL, 23 bytes

select x-mod(x,1)from t

Try it online!

Dr Y Wit

Posted 2019-08-22T23:26:14.953

Reputation: 511

0

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.

G. Sliepen

Posted 2019-08-22T23:26:14.953

Reputation: 580

0

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.

Draco18s no longer trusts SE

Posted 2019-08-22T23:26:14.953

Reputation: 3 053

0

Emacs Lisp, 9 bytes

'truncate

Try it online!

Jordon Biondo

Posted 2019-08-22T23:26:14.953

Reputation: 1 030

0

Kotlin, 9 bytes

x.toInt()

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

Alex.S

Posted 2019-08-22T23:26:14.953

Reputation: 101

3I 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 – 2019-08-23T19:23:50.663

1Um, isn't truncate shorter than 9 bytes... – Jo King – 2019-08-24T05:17:11.770

@JoKing Yes, but it's longer than toInt. – Alex.S – 2019-08-26T09:37:26.113

@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 – 2019-08-26T09:45:28.287

1Submissions 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 – 2019-08-26T09:53:08.927

As far as I can tell, toInt is a string method, so it has to be a lambda (I think), but truncate is just a function, so you can submit it as just truncate for 9 bytes

– Jo King – 2019-08-27T02:21:43.017

@JoKing toInt is also a float method. Does this mean that it's only 5 bytes?

– Alex.S – 2019-08-27T08:16:26.160

Can you do toInt(-5.3)? If so, yes. This also depends on whether Kotlin allows you to use a function as a value, like x = toInt; x(-5.3) or [1,2,3].map(toInt) – Jo King – 2019-08-27T09:58:37.893

@Alex.S You actually don't need the parenthesys for JavaScript, if you use ES6 template string (e.g.: parseInt\-5.5``). Template strings are string literals and some functions treat the numbers as string. In this case, parseInt is receiving a string (which is what it expects) and outputting an integer. Nothing wrong with the JavaScript solution. – Ismael Miguel – 2019-09-06T17:49:20.833

0

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.

seshoumara

Posted 2019-08-22T23:26:14.953

Reputation: 2 878

slight issue, -0.5 returns -0. – roblogic – 2019-08-26T23:54:12.113

@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 – 2019-08-27T23:51:34.320

0

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

Tau

Posted 2019-08-22T23:26:14.953

Reputation: 1 935

0

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.

Shaun Bebbers

Posted 2019-08-22T23:26:14.953

Reputation: 1 814

0

Visual Basic Script, 3 bytes

Fix

Full program, 20 bytes

MsgBox Fix(InputBox)

Sagittarius

Posted 2019-08-22T23:26:14.953

Reputation: 169

0

Julia (1.2), 5 bytes

trunc

Full program, 24 bytes

print(trunc(readline()))

Sagittarius

Posted 2019-08-22T23:26:14.953

Reputation: 169