43

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

JJTO
  • 847
  • 2
  • 8
  • 13
  • 3
    The code you've provided won't work, since `Int(x)` will return an `Inexact error` unless `x` is a whole number expressed as `Float64`, e.g. `1.0` or `-44.0`. Also, I can't tell what you're actually after based on the question. Your wording makes it sound like you want the decimal portion of a `Float64`, expressed as an `Int8`. Is this right? That is an odd request, particularly given that for your example number `1.23455`, the decimal portion as `Int64` is `23455`, but this is obviously *much* too large to be expressed as an `Int8`. – Colin T Bowers Nov 10 '16 at 05:09
  • Also, seems that you could use the rounding functions. All these functions accept target types for conversion: http://docs.julialang.org/en/release-0.5/manual/mathematical-operations/#man-rounding-functions – amrods Nov 10 '16 at 05:12

7 Answers7

44

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
Fengyang Wang
  • 11,901
  • 2
  • 38
  • 67
14

I think you are looking for floor:

julia> x = 1.23455
1.23455

julia> floor(x)
1.0

julia> y = x - floor(x)
0.23455000000000004
David P. Sanders
  • 5,210
  • 1
  • 23
  • 23
  • 11
    As of v0.6, note that the output of `floor` is not an `Int64` type, but rather a `Float64` as per the example: `test = ceil(0.2); typeof(test)` – jjjjjj Jan 17 '18 at 04:56
  • 7
    `floor(Int,x)` is more accurately according to [docs](https://docs.julialang.org/en/v1/base/math/#Base.floor) – Yousef Saber Feb 21 '20 at 16:41
5

To answer the general question in the title (convert Float to Int), we can do:

round(Int, 1.3)
# 1
round(Int, 1.7)
# 2
Timothée HENRY
  • 14,294
  • 21
  • 96
  • 136
  • It is recommended to use the alias `Int` instead of explicitly stating the size of the integer, see https://docs.julialang.org/en/v1/manual/types/#Type-Aliases-1 – Olov Mar 25 '20 at 13:07
  • 1
    I'm sorry, but not why do `round(Int64, 1.3)` instead? It's much more concise? What's the purpose of adding an explicit conversion? – Muppet Jul 31 '20 at 18:53
5

Combining the previous answers:

julia> int(x) = floor(Int, x)
int (generic function with 1 method)

julia> int(3.14)
3

julia> int(3.94)
3
Jabba
  • 19,598
  • 6
  • 52
  • 45
3

according to floor docs you can do that way

julia> x = 45.5
45.5

julia> typeof(x)
Float64

julia> x = floor(Int8,x)
45

julia> typeof(x)
Int8
Yousef Saber
  • 340
  • 2
  • 7
2

It seems that what you really need is the decimal part of the number. If this is the case, you can use modulo 1 directly like this:

x = 1.23455
y = x % 1
println(y)
# 0.2345
Javi12
  • 154
  • 1
  • 11
1

seems that you really need modf instead:

help?> modf
search: modf ComposedFunction mod1 mod module

  modf(x)

  Return a tuple (fpart, ipart) of the
  fractional and integral parts of a number.      
  Both parts have the same sign as the argument.  

  Examples
  ≡≡≡≡≡≡≡≡≡≡

  julia> modf(3.5)
  (0.5, 3.0)
  
  julia> modf(-3.5)
  (-0.5, -3.0)

longemen3000
  • 1,273
  • 5
  • 14