# Simpler Syntax For Conversion to integers

**URL:** <https://discourse.julialang.org/t/simpler-syntax-for-conversion-to-integers/12092>\
**Category:** New to Julia\
**Created:** [July 1, 2018, 6:24pm UTC](https://discourse.julialang.org/t/simpler-syntax-for-conversion-to-integers/12092 "2018-07-01T18:24:43Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Eli\_Hughes](https://avatars.discourse-cdn.com/v4/letter/e/a4c791/32.png) [@Eli\_Hughes](https://discourse.julialang.org/u/Eli_Hughes)\
**Post date:** [July 1, 2018, 6:24pm UTC](https://discourse.julialang.org/t/simpler-syntax-for-conversion-to-integers/12092/1 "2018-07-01T18:24:43Z")

</div>

I am trying to find some simpler syntax for variable type converters. In my scripts, I have been declaring variables for controlling the behavior like this:

```julia
FFT_Size = 128

```

I may then use this variable to control loops, create arrays, etc. The problem is that the numbers are treated as floats and I need them to be integers. For example in C, if I assign a float to an int, it will auto truncate the value. In many cases I need to divide this control variable by an integer.

For example:

```julia

Data = Array{Complex64}(convert(UInt32,floor(FFT_Size/2)))

```

I would like to simple do something like this:

```julia

Data = Array{Complex64}(FFT_Size/2)

```

In this case I am simple trying initialize an array of complex numbers with a length of the FFT\_Size. I am sure there is a _much_ simpler way of achieving this.

It does not seem like globals can have a type assigned like this:

```julia

FFT_Size::UInt32

```

Lastly it seems if I doing something like this:

```julia

a = 128::Int

```

the expression

```julia

a/2 or a/2::Int

```

will always yield a Float64

I often have to create new arrays where the length is dependent on some prior computation. Maybe I am simply approaching this incorrectly?

---

<div class="post-metadata">

**Author:** ![stillyslalom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stillyslalom/32/45687_2.png) [@stillyslalom](https://discourse.julialang.org/u/stillyslalom)\
**Post date:** [July 1, 2018, 6:38pm UTC](https://discourse.julialang.org/t/simpler-syntax-for-conversion-to-integers/12092/2 "2018-07-01T18:38:15Z")

</div>

Use `div`, which performs integer division. If you’re in the REPL or an editor with tab-completion, you can also use ÷ (typed as `\div <tab>`).

```julia
julia> 11 ÷ 4
2

julia> typeof(ans)
Int64

```

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [July 1, 2018, 7:48pm UTC](https://discourse.julialang.org/t/simpler-syntax-for-conversion-to-integers/12092/3 "2018-07-01T19:48:51Z")

</div>

> [@Eli\_Hughes](#):
>
> FFT\_Size = 128

`128` is an integer, and is treated as such. If you want a floating point number, you have to write `128.0`. That’s not your problem. What is hitting you is that `/` does ‘floating point division’. It has to, otherwise `1/2` wouldn’t give you `0.5`.

As @stillyslalom says, in order to keep the output as integers you have to explicitly ask for it, by using integer division, `div`.

---

<div class="post-metadata">

**Author:** ![jandehaan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jandehaan/32/6805_2.png) [@jandehaan](https://discourse.julialang.org/u/jandehaan)\
**Post date:** [July 1, 2018, 8:12pm UTC](https://discourse.julialang.org/t/simpler-syntax-for-conversion-to-integers/12092/4 "2018-07-01T20:12:39Z")

</div>

> [@Eli\_Hughes](#):
>
> Data = Array{Complex64}(convert(UInt32,floor(FFT\_Size/2)))

The `div` function also produces an integer by rounding down. If FFT\_Size happens to be odd I would venture that your `Data` array will be too short by 1.

---

<div class="post-metadata">

**Author:** ![dawbarton](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dawbarton/32/215461_2.png) [@dawbarton](https://discourse.julialang.org/u/dawbarton)\
**Post date:** [July 1, 2018, 8:41pm UTC](https://discourse.julialang.org/t/simpler-syntax-for-conversion-to-integers/12092/5 "2018-07-01T20:41:18Z")

</div>

Note that you can use `a::Int = 128` to ensure that `a` stays as an integer. In this case if you do floating point division you might run into some `InexactError`s. For example

```julia
julia> function div2(a)
       b::Int = a
       b = b/2
       return b
       end
div2 (generic function with 1 method)

julia> div2(128)
64

julia> div2(129)
ERROR: InexactError: Int64(Int64, 64.5)
Stacktrace:
 [1] Type at ./float.jl:692 [inlined]
 [2] convert at ./number.jl:7 [inlined]
 [3] div2(::Int64) at ./REPL[10]:3
 [4] top-level scope at none:0

```

---

<div class="post-metadata">

**Author:** ![Mattriks](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mattriks/32/351_2.png) [@Mattriks](https://discourse.julialang.org/u/Mattriks)\
**Post date:** [July 1, 2018, 10:48pm UTC](https://discourse.julialang.org/t/simpler-syntax-for-conversion-to-integers/12092/6 "2018-07-01T22:48:11Z")

</div>

```julia
floor(Int, 9/2)

```
