diff --git a/src/Decimals.jl b/src/Decimals.jl index 9709e76..10e3a2c 100644 --- a/src/Decimals.jl +++ b/src/Decimals.jl @@ -3,7 +3,7 @@ # @author jack@tinybike.net (Jack Peterson), 7/3/2014 module Decimals - import Base: ==, +, -, *, /, <, float, inv, round + import Base: ==, +, -, *, /, <, float, inv, round, trunc export Decimal, decimal, diff --git a/src/round.jl b/src/round.jl index c9f63b2..642485a 100644 --- a/src/round.jl +++ b/src/round.jl @@ -9,3 +9,14 @@ function round(x::Decimal; digits::Int=0, normal::Bool=false) (normal) ? d : normalize(d, rounded=true) end end + +function trunc(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.trunc(x.c / BigInt(10)^(-shift)) + d = Decimal(x.s, BigInt(c), x.q - shift) + (normal) ? d : normalize(d, rounded=true) + end +end diff --git a/test/test_round.jl b/test/test_round.jl index fa1577f..1cf0c16 100644 --- a/test/test_round.jl +++ b/test/test_round.jl @@ -11,6 +11,9 @@ using Test @test round.(Decimal.([0.1111, 0.2222, 0.8888]), digits=2) == Decimal.([0.11, 0.22, 0.89]) +@test trunc(Decimal(7.123456), digits=5) == Decimal(7.12345) + + function tet() a = parse(Decimal, "1.0000001") for i = 1:27