Convert float to int in Julia Lang

JJTO picture JJTO · Nov 10, 2016 · Viewed 33.9k times · Source

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

Answer

Fengyang Wang picture Fengyang Wang · Nov 11, 2016

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