# Round values for dataset

**URL:** <https://discourse.julialang.org/t/round-values-for-dataset/51159>\
**Category:** General Usage\
**Tags:** question, package, dataframes\
**Created:** [December 3, 2020, 12:49am UTC](https://discourse.julialang.org/t/round-values-for-dataset/51159 "2020-12-03T00:49:51Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![brett\_knoss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brett_knoss/32/13050_2.png) [@brett\_knoss](https://discourse.julialang.org/u/brett_knoss)\
**Post date:** [December 3, 2020, 12:49am UTC](https://discourse.julialang.org/t/round-values-for-dataset/51159/1 "2020-12-03T00:49:52Z")

</div>

```julia
x=1.000001
df=DataFrame(A=x)
for i in 1:10
x=x+1
push!(df,(x))
end

```

Is there a way to round this to say, 5 decimal places?, and to have that rounding thougout the dataframe?

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [December 3, 2020, 1:26am UTC](https://discourse.julialang.org/t/round-values-for-dataset/51159/2 "2020-12-03T01:26:40Z")

</div>

You want

```julia
floor(x, sigdigits = 5)

```

---

<div class="post-metadata">

**Author:** ![brett\_knoss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brett_knoss/32/13050_2.png) [@brett\_knoss](https://discourse.julialang.org/u/brett_knoss)\
**Post date:** [December 3, 2020, 2:39am UTC](https://discourse.julialang.org/t/round-values-for-dataset/51159/3 "2020-12-03T02:39:38Z")

</div>

where does that do I add that?

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [December 3, 2020, 3:06am UTC](https://discourse.julialang.org/t/round-values-for-dataset/51159/4 "2020-12-03T03:06:44Z")

</div>

I don’t know, wherever you would like. perhaps at the beginning?

```julia
x = round(1.00001, sigdigits = 5)

```

---

<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:** [December 3, 2020, 3:10am UTC](https://discourse.julialang.org/t/round-values-for-dataset/51159/5 "2020-12-03T03:10:47Z")

</div>

Here is an example with two values, one rounds down the other rounds up.  
I am showing this rounding to four digits after the decimal point rather than five digits, so Julia will display all digits even when showing them in `compact` mode. To do the same rounding to five digits after the decimal point, just change `4` to `5` in this line: `const fractional_digits = 4`.

```julia
using DataFrames
const fractional_digits = 4
x1 = 2.00004
x2 = 3.99996
xs = [x1, x2]
df = DataFrame(X=xs)
# the revised `:X` column gets assigned the values
# in the original `:X` column after rounding
df[!, :X] = round.(df[:, :X], digits=fractional_digits)
# notice the `.` after `round`, the `:` before `X`
# and notice the use of the keyword `digits`

```

which when run gives you

```julia
julia> using DataFrames
julia> const fractional_digits = 4
4
julia> x1 = 2.00004
2.00004
julia> x2 = 3.99996
3.99996
julia> xs = [x1, x2]
2-element Array{Float64,1}:
 2.00004
 3.99996

julia> df = DataFrame(X=xs)
2×1 DataFrame
 Row │ X
     │ Float64
─────┼─────────
   1 │ 2.00004
   2 │ 3.99996

julia> # notice the `.` after `round`, the `:` before `X`
julia> # and notice the use of the keyword `digits`
julia> df[!,:X] = round.(df[:,:X], digits=fractional_digits)
2-element Array{Float64,1}:
 2.0
 4.0

julia> df
2×1 DataFrame
 Row │ X
     │ Float64
─────┼─────────
   1 │ 2.0
   2 │ 4.0

```

Feel free to ask for an explanation of any part of that.

---

<div class="post-metadata">

**Author:** ![brett\_knoss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brett_knoss/32/13050_2.png) [@brett\_knoss](https://discourse.julialang.org/u/brett_knoss)\
**Post date:** [December 3, 2020, 6:31pm UTC](https://discourse.julialang.org/t/round-values-for-dataset/51159/6 "2020-12-03T18:31:09Z")

</div>

Is there supposed to be a period after digits?

---

<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:** [December 3, 2020, 6:34pm UTC](https://discourse.julialang.org/t/round-values-for-dataset/51159/7 "2020-12-03T18:34:14Z")

</div>

No. (assuming you are asking about the keyword argument `digits` that is used in the `round` function). I have edited the comments above to make it clearer.

No. (assuming you are asking about the assignment to `fractional_digits`). 🙂

---

<div class="post-metadata">

**Author:** ![brett\_knoss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brett_knoss/32/13050_2.png) [@brett\_knoss](https://discourse.julialang.org/u/brett_knoss)\
**Post date:** [December 3, 2020, 6:35pm UTC](https://discourse.julialang.org/t/round-values-for-dataset/51159/8 "2020-12-03T18:35:34Z")

</div>

> [@JeffreySarnoff](#):
>
> `const fractional_digits = 4`

yes, I figured you tested it, and when I copied your code it worked.

---

<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:** [December 3, 2020, 6:39pm UTC](https://discourse.julialang.org/t/round-values-for-dataset/51159/9 "2020-12-03T18:39:40Z")

</div>

It is good to have working code. It is better to have working code that you understand – so keep asking questions.

---

<div class="post-metadata">

**Author:** ![brett\_knoss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brett_knoss/32/13050_2.png) [@brett\_knoss](https://discourse.julialang.org/u/brett_knoss)\
**Post date:** [December 5, 2020, 9:14pm UTC](https://discourse.julialang.org/t/round-values-for-dataset/51159/10 "2020-12-05T21:14:04Z")

</div>

> [@JeffreySarnoff](#):
>
> `const fractional_digits = 4`

What does const fractional digits do?

---

<div class="post-metadata">

**Author:** ![brett\_knoss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brett_knoss/32/13050_2.png) [@brett\_knoss](https://discourse.julialang.org/u/brett_knoss)\
**Post date:** [December 5, 2020, 9:30pm UTC](https://discourse.julialang.org/t/round-values-for-dataset/51159/11 "2020-12-05T21:30:54Z")

</div>

What if I want 2 columns?

---

<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:** [December 7, 2020, 8:02pm UTC](https://discourse.julialang.org/t/round-values-for-dataset/51159/12 "2020-12-07T20:02:05Z")

</div>

It just lets you use `fractional_digits` as a variable name in this line  
`df[!, :X] = round.(df[:, :X], digits=fractional_digits)`

This is better practice than hard-coding the number `4` there.

---

<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:** [December 7, 2020, 8:02pm UTC](https://discourse.julialang.org/t/round-values-for-dataset/51159/13 "2020-12-07T20:02:42Z")

</div>

show me the two columns you want

---

<div class="post-metadata">

**Author:** ![brett\_knoss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brett_knoss/32/13050_2.png) [@brett\_knoss](https://discourse.julialang.org/u/brett_knoss)\
**Post date:** [December 7, 2020, 8:13pm UTC](https://discourse.julialang.org/t/round-values-for-dataset/51159/14 "2020-12-07T20:13:00Z")

</div>

makes sense
