# How to change datatype like MATLAB

**URL:** <https://discourse.julialang.org/t/how-to-change-datatype-like-matlab/84946>\
**Category:** General Usage\
**Tags:** matlab, doublefloats\
**Created:** [July 28, 2022, 10:18pm UTC](https://discourse.julialang.org/t/how-to-change-datatype-like-matlab/84946 "2022-07-28T22:18:28Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Starpower\_Power](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/starpower_power/32/38282_2.png) [@Starpower\_Power](https://discourse.julialang.org/u/Starpower_Power)\
**Post date:** [July 28, 2022, 10:18pm UTC](https://discourse.julialang.org/t/how-to-change-datatype-like-matlab/84946/1 "2022-07-28T22:18:28Z")

</div>

in matlab we can change datatype by double(22/7), is there any method in julia.  
Thank You

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [July 28, 2022, 10:31pm UTC](https://discourse.julialang.org/t/how-to-change-datatype-like-matlab/84946/2 "2022-07-28T22:31:06Z")

</div>

`22/7` is already “double” in Julia, it’s called `Float64`

```julia
julia> 22/7
3.142857142857143

julia> typeof(ans)
Float64

```

---

<div class="post-metadata">

**Author:** ![haberdashPI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/haberdashpi/32/26337_2.png) [@haberdashPI](https://discourse.julialang.org/u/haberdashPI)\
**Post date:** [July 28, 2022, 10:33pm UTC](https://discourse.julialang.org/t/how-to-change-datatype-like-matlab/84946/3 "2022-07-28T22:33:15Z")

</div>

It is not entirely clear what you mean: in Julia 22/7 will evaluate to a Float64 (a double in c). You can convert to other types by calling their constructor. For example Float32(22/7) would yield a single precision floating point value. Does that answer your question?

---

<div class="post-metadata">

**Author:** ![zdenek\_hurak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zdenek_hurak/32/53118_2.png) [@zdenek\_hurak](https://discourse.julialang.org/u/zdenek_hurak)\
**Post date:** [July 28, 2022, 10:37pm UTC](https://discourse.julialang.org/t/how-to-change-datatype-like-matlab/84946/4 "2022-07-28T22:37:35Z")

</div>

It is not perfectly clear what you want to achieve, because the usage of `double` in your example in Matlab does not really do much:

```julia
>> 22/7

ans =

    3.1429

>> class(ans)

ans =

    'double'

>> double(22/7)

ans =

    3.1429

>> class(ans)

ans =

    'double'

```

---

<div class="post-metadata">

**Author:** ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)\
**Post date:** [July 29, 2022, 2:10am UTC](https://discourse.julialang.org/t/how-to-change-datatype-like-matlab/84946/5 "2022-07-29T02:10:15Z")

</div>

As the others have said, in this specific example, the object is already Float64, aka C-double.

In general to cast an object to be of a different tye use `objTypeT = convert(T,objToConvert)` where `T` is the desired type.

Of course someone must have write a function to convert something from the original type to `T`…

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [July 29, 2022, 9:29am UTC](https://discourse.julialang.org/t/how-to-change-datatype-like-matlab/84946/6 "2022-07-29T09:29:46Z")

</div>

There are several floating point types in Julia.

- `single` in MATLAB is the equivalent of `Float32`
- `double` in MATLAB is the equivalent of `Float64`

Additionally, there are

- `Float16` sometimes called `half`
- `BigFloat` arbitrary precision floating point

```julia
julia> subtypes(AbstractFloat)
4-element Vector{Any}:
 BigFloat
 Float16
 Float32
 Float64

julia> p = 22/7
3.142857142857143

julia> typeof(p)
Float64

julia> p32 = Float32(p)
3.142857f0

julia> typeof(p32)
Float32

julia> p16 = Float16(p)
Float16(3.143)

julia> typeof(p16)
Float16

```

The `convert(Float32, p)` example shown above is a more general way to do this for other types.

Note that if you have an array, you may want to use broadcasting to convert all elements of the array to the desired type.

```julia
julia> a = [1,2,3,4]
4-element Vector{Int64}:
 1
 2
 3
 4

julia> Float32.(a)
4-element Vector{Float32}:
 1.0
 2.0
 3.0
 4.0

```

There are some related types.

- `Rational` used to represent a number that is the quotient of two integers.
- `Irrational` used to represent a number that cannot be expressed as a quotient of two integers

The method `float` can be used to convert them to some kind of floating point value.

```julia
julia> r = 22//7
22//7

julia> typeof(r)
Rational{Int64}

julia> float(r)
3.142857142857143

julia> typeof(ans)
Float64

julia> pi
π = 3.1415926535897...

julia> typeof(pi)
Irrational{:π}

```

Floating point can yield some strange results:

```julia
julia> 0.1 + 0.2
0.30000000000000004

julia> big"0.1" + big"0.2"
0.3000000000000000000000000000000000000000000000000000000000000000000000000000017

julia> typeof(ans)
BigFloat

```

You can use rationals to help:

```julia
julia> 1//10 + 2//10
3//10

julia> float(ans)
0.3

```

There are some additional packages that can help:

```julia
julia> using FixedPointDecimals

julia> FixedDecimal{Int, 1}(0.1) + FixedDecimal{Int, 1}(0.2)
FixedDecimal{Int64,1}(0.3)

julia> float(ans)
0.3

```
