# Significant digits in an csv file

**URL:** <https://discourse.julialang.org/t/significant-digits-in-an-csv-file/79430>\
**Category:** New to Julia\
**Tags:** numbers, csv\
**Created:** [April 13, 2022, 11:22am UTC](https://discourse.julialang.org/t/significant-digits-in-an-csv-file/79430 "2022-04-13T11:22:38Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![DaWa](https://avatars.discourse-cdn.com/v4/letter/d/46a35a/32.png) [@DaWa](https://discourse.julialang.org/u/DaWa)\
**Post date:** [April 13, 2022, 11:22am UTC](https://discourse.julialang.org/t/significant-digits-in-an-csv-file/79430/1 "2022-04-13T11:22:38Z")

</div>

Hello all,

I am reading in a lot of CSV files and create a DF using

`DataFrame(CSV.File("C:/Users/.../$id.csv", header=["date", "tmax"]))`

Now I would like to have the number of significant digits of tmax as a new column of my df. Since tmax are Floats ndigit does not work. Is there a way to easily do this?

Thanks, Daniel

---

<div class="post-metadata">

**Author:** ![jd-foster](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jd-foster/32/35824_2.png) [@jd-foster](https://discourse.julialang.org/u/jd-foster)\
**Post date:** [April 13, 2022, 12:33pm UTC](https://discourse.julialang.org/t/significant-digits-in-an-csv-file/79430/2 "2022-04-13T12:33:28Z")

</div>

Try `length(digits(x))` where `x` is one or both of the two parts obtained from `Base.Math.modf`

---

<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:** [April 13, 2022, 12:43pm UTC](https://discourse.julialang.org/t/significant-digits-in-an-csv-file/79430/3 "2022-04-13T12:43:42Z")

</div>

FWIW, in case this helps.  
The numbers are loaded as strings for processing the digits and parsed to floats later:

```julia
using CSV, DataFrames

const input="""
date,tmax
1-Jan-2000,5.7E3
2-Feb-2001,31.88700
3-Mar-2002,100e0
4-Apr-2003,100.1
"""

df = DataFrame(CSV.File(IOBuffer(input), types=Dict(:date=>String, :tmax=>String)))

str = split.(string.(first.(split.(lowercase.(df.tmax),'e'))),'.')
df.sdigs = zeros(Int, nrow(df))
for (i,s) in pairs(str)
    df.sdigs[i] = length(s[1])
    (length(s)>1) && (df.sdigs[i] += length(rstrip(s[2],'0')))
end
df.tmax = parse.(Float64, df.tmax)
df

# Result:
 Row │ date tmax sdigs 
     │ String Float64 Int64
─────┼─────────────────────────────
   1 │ 1-Jan-2000 5700.0 2
   2 │ 2-Feb-2001 31.887 5
   3 │ 3-Mar-2002 100.0 3
   4 │ 4-Apr-2003 100.1 4

```

---

<div class="post-metadata">

**Author:** ![DaWa](https://avatars.discourse-cdn.com/v4/letter/d/46a35a/32.png) [@DaWa](https://discourse.julialang.org/u/DaWa)\
**Post date:** [April 13, 2022, 2:03pm UTC](https://discourse.julialang.org/t/significant-digits-in-an-csv-file/79430/4 "2022-04-13T14:03:24Z")

</div>

Thanks to both of you for the reply. I think [jd-foster](https://discourse.julialang.org/u/jd-foster)s answer does not work, since `Base.Math.modf` still returns floats. Also there would be issues with 0 at the end and numbers like 1.1.

[rafael.guerra](https://discourse.julialang.org/u/rafael.guerra) I really like your answer. I am not sure about  
(length(s)\>1) && (df.sdigs[i] += length(rstrip(s[2],‘0’)))

since that removes significant 0’s at the end, doesn’t it?

---

<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:** [April 13, 2022, 2:16pm UTC](https://discourse.julialang.org/t/significant-digits-in-an-csv-file/79430/5 "2022-04-13T14:16:33Z")

</div>

Daniel (@DaWA), yes that line is there to remove trailing zeros from the fractional part of numbers like `31.88700`, but maybe those should be kept?

Frankly, the code was just to give some ideas, as the general problem is beyond me.

---

<div class="post-metadata">

**Author:** ![DaWa](https://avatars.discourse-cdn.com/v4/letter/d/46a35a/32.png) [@DaWa](https://discourse.julialang.org/u/DaWa)\
**Post date:** [April 13, 2022, 3:32pm UTC](https://discourse.julialang.org/t/significant-digits-in-an-csv-file/79430/6 "2022-04-13T15:32:49Z")

</div>

All right, thanks alot! So yeah those 0’s are significant and should be kept. Your code works great. Really helped me. Also very nice to already include a solution for scientific number notation!
