# Get the decimal part of a number

**URL:** <https://discourse.julialang.org/t/get-the-decimal-part-of-a-number/78282>\
**Category:** General Usage\
**Tags:** numbers\
**Created:** [March 22, 2022, 3:29pm UTC](https://discourse.julialang.org/t/get-the-decimal-part-of-a-number/78282 "2022-03-22T15:29:51Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![CameronBieganek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cameronbieganek/32/6915_2.png) [@CameronBieganek](https://discourse.julialang.org/u/CameronBieganek)\
**Post date:** [March 22, 2022, 3:29pm UTC](https://discourse.julialang.org/t/get-the-decimal-part-of-a-number/78282/1 "2022-03-22T15:29:51Z")

</div>

Is there a function in Base Julia to get the decimal part of a number? I feel like I’ve seen that function before, but now I can’t find it. For example, I would like the following:

```julia
julia> x = 12.345;

julia> foo(x)
0.345

```

Granted, I can get an approximation to that behavior, but I’m looking for a builtin function. If I try `x - floor(x)`, then sometimes I get some floating point wonkiness:

```julia
julia> x = 1.3
1.3

julia> x - floor(x)
0.30000000000000004

```

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [March 22, 2022, 3:33pm UTC](https://discourse.julialang.org/t/get-the-decimal-part-of-a-number/78282/2 "2022-03-22T15:33:13Z")

</div>

This isn’t floating point wonkiness. This is just that `1.3 != 13//10` The exact value of `1.3 - 1` is `0.30000000000000004`.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [March 22, 2022, 3:38pm UTC](https://discourse.julialang.org/t/get-the-decimal-part-of-a-number/78282/3 "2022-03-22T15:38:17Z")

</div>

> [@CameronBieganek](#):
>
> ```julia
> julia> x = 1.3
> 1.3
> 
> ```

Realize that Julia isn’t printing all of the digits here, just the smallest number of digits that rounds to the same value (because `1.3` isn’t exactly represented in binary floating point). If you print all of the digits you see:

```julia
julia> big(1.3)
1.3000000000000000444089209850062616169452667236328125

```

So `1.3 - 1` (in `Float64`) is exactly `0.3000000000000000444089209850062616169452667236328125`, which gets printed as `0.30000000000000004` for `Float64` (double precision). (`0.3xxx` needs to print one more decimal digit at the end to ensure that it rounds to the same binary-floating point value, because the number of significant decimal places has increased by one compared to `1.3xxx`.)

In contrast, `0.3` in double precision rounds to a _different_ binary floating-point value:

```julia
julia> big(0.3)
0.299999999999999988897769753748434595763683319091796875

```

Therefore, `1.3 - 1 != 0.3` is correct.

If you need exact decimals for some reason, you can use [decimal floating point via the DecFP.l package](https://github.com/JuliaMath/DecFP.jl):

```julia
julia> using DecFP

julia> x = d"1.3"
1.3

julia> x - floor(x)
0.3

```

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [March 22, 2022, 3:44pm UTC](https://discourse.julialang.org/t/get-the-decimal-part-of-a-number/78282/4 "2022-03-22T15:44:14Z")

</div>

See also [PSA: floating-point arithmetic](https://discourse.julialang.org/t/psa-floating-point-arithmetic/8678)

---

<div class="post-metadata">

**Author:** ![CameronBieganek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cameronbieganek/32/6915_2.png) [@CameronBieganek](https://discourse.julialang.org/u/CameronBieganek)\
**Post date:** [March 22, 2022, 3:52pm UTC](https://discourse.julialang.org/t/get-the-decimal-part-of-a-number/78282/5 "2022-03-22T15:52:44Z")

</div>

> [@Oscar\_Smith](#):
>
> This isn’t floating point wonkiness.

By “floating point wonkiness”, I mean floating point numbers behaving correctly but counter-intuitively. Obviously floating point numbers in Julia conform to the IEEE standard.

I thought I had once used the hypothetical function mentioned in my original post, but it must have been a dream I had.

---

<div class="post-metadata">

**Author:** ![pixel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pixel/32/26437_2.png) [@pixel](https://discourse.julialang.org/u/pixel)\
**Post date:** [March 22, 2022, 7:11pm UTC](https://discourse.julialang.org/t/get-the-decimal-part-of-a-number/78282/6 "2022-03-22T19:11:19Z")

</div>

The ‘modf’ function returns a tuple containing fractional and integer parts of a number. Maybe that could be useful to you?

---

<div class="post-metadata">

**Author:** ![haina](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/haina/32/44692_2.png) [@haina](https://discourse.julialang.org/u/haina)\
**Post date:** [January 14, 2023, 11:55pm UTC](https://discourse.julialang.org/t/get-the-decimal-part-of-a-number/78282/8 "2023-01-14T23:55:32Z")

</div>

`mod(x, 1)` works.

---

<div class="post-metadata">

**Author:** ![uniment](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/uniment/32/24532_2.png) [@uniment](https://discourse.julialang.org/u/uniment)\
**Post date:** [January 15, 2023, 2:14am UTC](https://discourse.julialang.org/t/get-the-decimal-part-of-a-number/78282/9 "2023-01-15T02:14:07Z")

</div>

Not for negative numbers.

```julia
julia> let xs=(3.14, -3.14); modf.(xs) end
((0.14000000000000012, 3.0), (-0.14000000000000012, -3.0))

julia> let xs=(3.14, -3.14); mod.(xs, 1) end
(0.14000000000000012, 0.8599999999999999)

```

However, `mod(x, sign(x))` works:

```julia
julia> let xs=(3.14, -3.14); mod.(xs, sign.(xs)) end
(0.14000000000000012, -0.14000000000000012)

```
