Convert float to int in Julia Lang
Asked Answered
L

8

46

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

Laughlin answered 10/11, 2016 at 4:47 Comment(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.Earwitness
Also, seems that you could use the rounding functions. All these functions accept target types for conversion: docs.julialang.org/en/release-0.5/manual/…Stantonstanway
You can use a modulo operation to get the fractional part 1.23455 % 1.Simplex
P
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
Planography answered 10/11, 2016 at 5:20 Comment(2)
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)Salivation
floor(Int,x) is more accurately according to docsForcible
F
47

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
Fernand answered 11/11, 2016 at 0:30 Comment(3)
trunc returns Float now, not Integer.Sialkot
@Sialkot trunc(x::Float64) has always returned Float. But if you provide Int as the first argument, as in trunc(Int, 1.2), you will get an integer.Fernand
This should be the best answerRohrer
P
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
Planography answered 10/11, 2016 at 5:20 Comment(2)
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)Salivation
floor(Int,x) is more accurately according to docsForcible
A
7

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
Amendatory answered 23/10, 2021 at 8:27 Comment(0)
T
6

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
Thundering answered 6/2, 2020 at 8:24 Comment(2)
It is recommended to use the alias Int instead of explicitly stating the size of the integer, see docs.julialang.org/en/v1/manual/types/#Type-Aliases-1Parthenopaeus
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?Deceit
F
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
Forcible answered 21/2, 2020 at 16:34 Comment(0)
N
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
Neoplasticism answered 21/7, 2020 at 22:26 Comment(0)
S
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)

Seventh answered 26/10, 2021 at 1:46 Comment(0)
T
1

If you want to convert Float to Int, this is a two-step operation. Because you must truncate the decimal part before typecasting it:

i = Int(trunc(1.23455))

Otherwise you can use modf to split a floating number into a tuple containing two floats that hold the integral and fractional parts of the original number.

(i, p) = modf(1.23455)
Titanic answered 7/10, 2023 at 5:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.