# Getting first digit of an integer(big or small)

**URL:** <https://discourse.julialang.org/t/getting-first-digit-of-an-integer-big-or-small/66636>\
**Category:** New to Julia\
**Tags:** math\
**Created:** [August 18, 2021, 10:37pm UTC](https://discourse.julialang.org/t/getting-first-digit-of-an-integer-big-or-small/66636 "2021-08-18T22:37:06Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ogweno\_Emmanuel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ogweno_emmanuel/32/35482_2.png) [@Ogweno\_Emmanuel](https://discourse.julialang.org/u/Ogweno_Emmanuel)\
**Post date:** [August 18, 2021, 10:37pm UTC](https://discourse.julialang.org/t/getting-first-digit-of-an-integer-big-or-small/66636/1 "2021-08-18T22:37:07Z")

</div>

I’m looking for a way to get the first digit of any integer. I’ve seen the digits function a lot on the net but it’s not working on my end. Could it be that there is a package I ought to add?  
Also, how can I easily get the first digit of any integer and still maintain it as an integer?  
I plan to use the integers for some calculations.

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [August 18, 2021, 10:58pm UTC](https://discourse.julialang.org/t/getting-first-digit-of-an-integer-big-or-small/66636/2 "2021-08-18T22:58:52Z")

</div>

Assuming you want the most significant digit …

```julia
function firstdigit(x::Integer)
  iszero(x) && return x
  x = abs(x)
  y = 10^floor(Int, log10(x))
  z = div(x, y)
  return z
end

```

---

<div class="post-metadata">

**Author:** ![dlakelan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlakelan/32/8491_2.png) [@dlakelan](https://discourse.julialang.org/u/dlakelan)\
**Post date:** [August 18, 2021, 11:04pm UTC](https://discourse.julialang.org/t/getting-first-digit-of-an-integer-big-or-small/66636/3 "2021-08-18T23:04:07Z")

</div>

or if you don’t want to call the log10 function you can do:

```julia
z=abs(x)
while z >= 10
   z = div(z,10)
end
return z

```

---

<div class="post-metadata">

**Author:** ![Ogweno\_Emmanuel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ogweno_emmanuel/32/35482_2.png) [@Ogweno\_Emmanuel](https://discourse.julialang.org/u/Ogweno_Emmanuel)\
**Post date:** [August 19, 2021, 7:18am UTC](https://discourse.julialang.org/t/getting-first-digit-of-an-integer-big-or-small/66636/4 "2021-08-19T07:18:32Z")

</div>

This works, thanks a lot @JeffreySarnoff

---

<div class="post-metadata">

**Author:** ![Ogweno\_Emmanuel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ogweno_emmanuel/32/35482_2.png) [@Ogweno\_Emmanuel](https://discourse.julialang.org/u/Ogweno_Emmanuel)\
**Post date:** [August 19, 2021, 7:34am UTC](https://discourse.julialang.org/t/getting-first-digit-of-an-integer-big-or-small/66636/5 "2021-08-19T07:34:00Z")

</div>

I would now like to count the number of digits that are 1, 2, 3, …, 10 in an array of these firstdigits. How can I do that?

---

<div class="post-metadata">

**Author:** ![mcabbott](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcabbott/32/6603_2.png) [@mcabbott](https://discourse.julialang.org/u/mcabbott)\
**Post date:** [August 19, 2021, 8:07am UTC](https://discourse.julialang.org/t/getting-first-digit-of-an-integer-big-or-small/66636/6 "2021-08-19T08:07:22Z")

</div>

Surely there are library functions which will be faster, but one simple way is:

```julia
julia> function benford(ns)
         xs = firstdigit.(ns)
         [d => count(isequal(d), xs) for d in 0:9]
       end;

julia> benford(trunc.(Int, abs.(1000 .* randn(1000))))
10-element Vector{Pair{Int64, Int64}}:
 0 => 1
 1 => 367
 2 => 114
 3 => 90
 4 => 80
 5 => 60
...

```

---

<div class="post-metadata">

**Author:** ![Ogweno\_Emmanuel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ogweno_emmanuel/32/35482_2.png) [@Ogweno\_Emmanuel](https://discourse.julialang.org/u/Ogweno_Emmanuel)\
**Post date:** [August 19, 2021, 8:15am UTC](https://discourse.julialang.org/t/getting-first-digit-of-an-integer-big-or-small/66636/7 "2021-08-19T08:15:50Z")

</div>

Thank you @mcabbott. This works just fine.

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [August 19, 2021, 8:17am UTC](https://discourse.julialang.org/t/getting-first-digit-of-an-integer-big-or-small/66636/8 "2021-08-19T08:17:30Z")

</div>

> [@mcabbott](#):
>
> `benford`

It might be worth point out to this article: [Benford’s law](https://en.wikipedia.org/wiki/Benford%27s_law)

---

<div class="post-metadata">

**Author:** ![cormullion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cormullion/32/49131_2.png) [@cormullion](https://discourse.julialang.org/u/cormullion)\
**Post date:** [August 19, 2021, 8:19am UTC](https://discourse.julialang.org/t/getting-first-digit-of-an-integer-big-or-small/66636/9 "2021-08-19T08:19:17Z")

</div>

`digits` works for me:

```julia
map(x -> last(digits(x)), (12, 13, 125, 36, 46, 57, 78))
(1, 1, 1, 3, 4, 5, 7)

```

The most significant digits are at the end of the array.

---

<div class="post-metadata">

**Author:** ![Ogweno\_Emmanuel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ogweno_emmanuel/32/35482_2.png) [@Ogweno\_Emmanuel](https://discourse.julialang.org/u/Ogweno_Emmanuel)\
**Post date:** [August 19, 2021, 8:27am UTC](https://discourse.julialang.org/t/getting-first-digit-of-an-integer-big-or-small/66636/10 "2021-08-19T08:27:14Z")

</div>

Yes, this is what I’m working on (or at least a generic version of it). Using that Benford’s function, it creates an array of pairs. But I only want to use the second part of the pair i.e the number of counts each digit appears. How can I do that?

---

<div class="post-metadata">

**Author:** ![Ogweno\_Emmanuel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ogweno_emmanuel/32/35482_2.png) [@Ogweno\_Emmanuel](https://discourse.julialang.org/u/Ogweno_Emmanuel)\
**Post date:** [August 19, 2021, 8:34am UTC](https://discourse.julialang.org/t/getting-first-digit-of-an-integer-big-or-small/66636/11 "2021-08-19T08:34:11Z")

</div>

Or a better way would be how to plot pairs against each other.

 ![benford](https://global.discourse-cdn.com/julialang/original/3X/8/7/8762b241facbdf830f1d8c9076758a08bc634ab9.png)  
How can I plot the left pairs against the right?

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [August 19, 2021, 8:35am UTC](https://discourse.julialang.org/t/getting-first-digit-of-an-integer-big-or-small/66636/12 "2021-08-19T08:35:58Z")

</div>

You can try the bar plot from Plots.jl:

```julia
x = benford(trunc.(Int, abs.(1000 .* randn(1000))))
bar(first.(x),last.(x), xticks = (0:9, string.(0:9)))

```

---

<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:** [August 19, 2021, 8:43am UTC](https://discourse.julialang.org/t/getting-first-digit-of-an-integer-big-or-small/66636/13 "2021-08-19T08:43:29Z")

</div>

> [@Ogweno\_Emmanuel](#):
>
> This works, thanks a lot @JeffreySarnoff

Just in case performance matters: the solution from @dlakelan is significantly faster, and, I suspect, close to theoretically optimal.

---

<div class="post-metadata">

**Author:** ![Ogweno\_Emmanuel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ogweno_emmanuel/32/35482_2.png) [@Ogweno\_Emmanuel](https://discourse.julialang.org/u/Ogweno_Emmanuel)\
**Post date:** [August 19, 2021, 8:44am UTC](https://discourse.julialang.org/t/getting-first-digit-of-an-integer-big-or-small/66636/14 "2021-08-19T08:44:14Z")

</div>

> [@rafael.guerra](#):
>
> x = benford(trunc.(Int, abs.(1000 .\* randn(1000))))

Thank you. It worked. Though my x = benford(firstdigits) because I want it to work for any number that comes into my first digits function. The first digits function takes input from a computation that works out the collatz conjencture for any number

---

<div class="post-metadata">

**Author:** ![Ogweno\_Emmanuel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ogweno_emmanuel/32/35482_2.png) [@Ogweno\_Emmanuel](https://discourse.julialang.org/u/Ogweno_Emmanuel)\
**Post date:** [August 19, 2021, 8:46am UTC](https://discourse.julialang.org/t/getting-first-digit-of-an-integer-big-or-small/66636/15 "2021-08-19T08:46:38Z")

</div>

Thank you. Now that I have the logic right, I’ll look into it for performance

---

<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:** [August 19, 2021, 9:09am UTC](https://discourse.julialang.org/t/getting-first-digit-of-an-integer-big-or-small/66636/16 "2021-08-19T09:09:22Z")

</div>

> [@Ogweno\_Emmanuel](#):
>
> Now that I have the logic right, I’ll look into it for performance

Good point! I always look for performance first, correctness second 😁
