Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert float to int in Julia Lang

Is there a way to convert a floating number to int in Julia? I'm trying to convert a floating point number to a fixed precision number with the decimal part represented as 8bit integer. In order to do this, I need to truncate just the decimal part of the number and I figured the best way to do this would be to subtract the converted integer of x from floating point x:

  x = 1.23455 y = x - Int(x) println(y) 

y = 0.23455

like image 240
JJTO Avatar asked Nov 10 '16 04:11

JJTO


1 Answers

It is possible that you are looking for trunc. It depends on what you mean by the decimal part. This is the difference between trunc and floor:

julia> trunc(Int, 1.2) 1  julia> trunc(Int, -1.2) -1  julia> floor(Int, 1.2) 1  julia> floor(Int, -1.2) -2 
like image 100
Fengyang Wang Avatar answered Sep 22 '22 18:09

Fengyang Wang