Skip to content

Commit

Permalink
Make digits a keyword argument to round
Browse files Browse the repository at this point in the history
In Julia 0.7, the digits positional argument to `round` was deprecated
in favor of a `digits=` keyword argument. This introduces a similar
change here.
  • Loading branch information
ararslan committed Jun 26, 2018
1 parent be5eacc commit 77b9361
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 16 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -132,7 +132,7 @@ Inequality:

Rounding:

julia> round(decimal(3.1415), 2)
julia> round(decimal(3.1415), digits=2)
Decimal(0,314,-2)

julia> string(ans)
Expand Down
4 changes: 2 additions & 2 deletions src/Decimals.jl
Expand Up @@ -4,14 +4,14 @@
__precompile__()

module Decimals
import Base: ==, +, -, *, /, <, float, norm, inv
import Base: ==, +, -, *, /, <, float, norm, inv, round

export Decimal,
decimal,
decimal,
number

DIGITS = 20
const DIGITS = 20

# Numerical value: (-1)^s * c * 10^q
struct Decimal <: Real
Expand Down
2 changes: 1 addition & 1 deletion src/norm.jl
Expand Up @@ -11,7 +11,7 @@ function Base.normalize(x::Decimal; rounded::Bool=false)
if rounded
Decimal(x.s, abs(c), q)
else
round(Decimal(x.s, abs(c), q), DIGITS; normal=true)
round(Decimal(x.s, abs(c), q), digits=DIGITS, normal=true)
end
end

Expand Down
12 changes: 7 additions & 5 deletions src/round.jl
@@ -1,11 +1,13 @@
# Rounding
function Base.round(x::Decimal, dpts::Int=0; normal::Bool=false)
shift = BigInt(dpts) + x.q
function round(x::Decimal; digits::Int=0, normal::Bool=false)
shift = BigInt(digits) + x.q
if shift > BigInt(0) || shift < x.q
(normal) ? x : normalize(x, rounded=true)
else
c = Base.round(x.c / BigInt(10)^(-shift))
d = Decimal(x.s, BigInt(c), x.q - shift)
(normal) ? d : normalize(d, rounded=true)
c = Base.round(x.c / BigInt(10)^(-shift))
d = Decimal(x.s, BigInt(c), x.q - shift)
(normal) ? d : normalize(d, rounded=true)
end
end

@deprecate round(x::Decimal, dpts::Int; normal::Bool=false) round(x, digits=dpts, normal=normal)
13 changes: 6 additions & 7 deletions test/test_round.jl
Expand Up @@ -3,14 +3,13 @@ using Compat.Test

@testset "Rounding" begin

@test round(Decimal(7.123456), 0) == Decimal(7)
@test round(Decimal(7.123456), 2) == Decimal(7.12)
@test round(Decimal(7.123456), 3) == Decimal(7.123)
@test round(Decimal(7.123456), 5) == Decimal(7.12346)
@test round(Decimal(7.123456), 6) == Decimal(7.123456)
@test round(Decimal(7.123456), digits=0) == Decimal(7)
@test round(Decimal(7.123456), digits=2) == Decimal(7.12)
@test round(Decimal(7.123456), digits=3) == Decimal(7.123)
@test round(Decimal(7.123456), digits=5) == Decimal(7.12346)
@test round(Decimal(7.123456), digits=6) == Decimal(7.123456)

@test round(0.123, 1) == 0.1
@test round.([0.1111 0.2222 0.8888], 2) == [0.11 0.22 0.89]
@test round.(Decimal.([0.1111, 0.2222, 0.8888]), digits=2) == Decimal.([0.11, 0.22, 0.89])

function tet()
a = parse(Decimal, "1.0000001")
Expand Down

0 comments on commit 77b9361

Please sign in to comment.