Skip to content

Commit f468058

Browse files
committed
Fix show displaying wrong value
1 parent 4dd5492 commit f468058

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

src/FixedPointDecimals.jl

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ function print{T, f}(io::IO, x::FD{T, f})
283283

284284
# note: a is negative if x.i == typemin(x.i)
285285
s, a = sign(x.i), abs(x.i)
286-
integer, fractional = divrem(a, T(10)^f)
286+
integer, fractional = divrem(a, exp10(T, f))
287287
integer = abs(integer) # ...but since f > 0, this is positive
288288
fractional = abs(fractional)
289289

@@ -382,4 +382,15 @@ function parse_round{T}(::Type{T}, fractional::AbstractString, ::RoundingMode{:N
382382
return T(0)
383383
end
384384

385+
function exp10_max{T}(::Type{T})
386+
length(digits(typemax(T))) - 1
387+
end
388+
389+
function exp10(T::Type, y::Integer)
390+
while T != BigInt && exp10_max(T) < y
391+
T = widen(T)
392+
end
393+
T(10)^y
394+
end
395+
385396
end

test/runtests.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,12 @@ end
424424
@test string(FD2(-0.01)) == "-0.01"
425425
@test string(FD2(0)) == "0.00"
426426
@test string(FixedDecimal{Int,0}(123.4)) == "123"
427+
428+
# Displaying a decimal could be incorrect when using a decimal place precision which is
429+
# close to or at the limit the limit for our storage type. Int32 for example can only
430+
# store up to 10 digits of precision.
431+
@test string(reinterpret(FD{Int32,10}, typemax(Int32))) == "0.2147483647"
432+
@test string(reinterpret(FD{Int32,10}, typemin(Int32))) == "-0.2147483648"
427433
end
428434

429435
@testset "show" begin

0 commit comments

Comments
 (0)