Skip to content

Commit c6e2dc7

Browse files
committed
Made print method for FixedDecimal (#75)
Allows users to just print the numeric representation to a buffer.
1 parent d7c2d5c commit c6e2dc7

File tree

2 files changed

+34
-23
lines changed

2 files changed

+34
-23
lines changed

src/FixedPointDecimals.jl

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ using Compat
2929

3030
import Base: reinterpret, zero, one, abs, sign, ==, <, <=, +, -, /, *, div,
3131
rem, divrem, fld, mod, fldmod, fld1, mod1, fldmod1, isinteger,
32-
typemin, typemax, realmin, realmax, show, convert, promote_rule,
33-
min, max, trunc, round, floor, ceil, eps, float, widemul
32+
typemin, typemax, realmin, realmax, print, show, string, convert,
33+
promote_rule, min, max, trunc, round, floor, ceil, eps, float, widemul
3434

3535
"""
3636
FixedDecimal{I <: Integer, f::Int}
@@ -223,17 +223,10 @@ realmin{T <: FD}(::Type{T}) = eps(T)
223223
realmax{T <: FD}(::Type{T}) = typemax(T)
224224

225225
# printing
226-
function show{T}(io::IO, x::FD{T, 0})
227-
iscompact = get(io, :compact, false)
228-
if !iscompact
229-
print(io, "FixedDecimal{$T,0}(")
230-
end
226+
function print{T}(io::IO, x::FD{T, 0})
231227
print(io, x.i)
232-
if !iscompact
233-
print(io, ')')
234-
end
235228
end
236-
function show{T, f}(io::IO, x::FD{T, f})
229+
function print{T, f}(io::IO, x::FD{T, f})
237230
iscompact = get(io, :compact, false)
238231

239232
# note: a is negative if x.i == typemin(x.i)
@@ -242,9 +235,6 @@ function show{T, f}(io::IO, x::FD{T, f})
242235
integer = abs(integer) # ...but since f > 0, this is positive
243236
fractional = abs(fractional)
244237

245-
if !iscompact
246-
print(io, "FixedDecimal{$T,$f}(")
247-
end
248238
if s == -1
249239
print(io, "-")
250240
end
@@ -256,6 +246,14 @@ function show{T, f}(io::IO, x::FD{T, f})
256246
end
257247
end
258248
print(io, integer, '.', fractionchars)
249+
end
250+
251+
function show{T, f}(io::IO, x::FD{T, f})
252+
iscompact = get(io, :compact, false)
253+
if !iscompact
254+
print(io, "FixedDecimal{$T,$f}(")
255+
end
256+
print(io, x)
259257
if !iscompact
260258
print(io, ')')
261259
end

test/runtests.jl

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,16 @@ epsi{T}(::Type{T}) = eps(T)
345345
end
346346
end
347347

348+
@testset "print" begin
349+
@test string(FD2(1.00)) == "1.00"
350+
@test string(FD2(1.23)) == "1.23"
351+
@test string(FD2(42.40)) == "42.40"
352+
@test string(FD2(-42.40)) == "-42.40"
353+
@test string(FD2(-0.01)) == "-0.01"
354+
@test string(FD2(0)) == "0.00"
355+
@test string(FixedDecimal{Int,0}(123.4)) == "123"
356+
end
357+
348358
@testset "show" begin
349359
@testset "compact" begin
350360
@test sprint(showcompact, FD2(1.00)) == "1.0"
@@ -353,19 +363,22 @@ end
353363
@test sprint(showcompact, FD2(-42.40)) == "-42.4"
354364
@test sprint(showcompact, FD2(-0.01)) == "-0.01"
355365
@test sprint(showcompact, FD2(0)) == "0.0"
356-
for x in keyvalues[FD2]
357-
if 0 abs(x) < 1000
358-
@test eval(parse(string(x))) == x
359-
end
360-
end
361366

362-
@test string(typemin(FixedDecimal{Int64, 2})) ==
367+
@test repr(typemin(FixedDecimal{Int64, 2})) ==
363368
"FixedDecimal{Int64,2}(-92233720368547758.08)"
364-
@test string(typemax(FixedDecimal{Int64, 2})) ==
369+
@test repr(typemax(FixedDecimal{Int64, 2})) ==
365370
"FixedDecimal{Int64,2}(92233720368547758.07)"
366-
@test string(typemin(FixedDecimal{Int32, 2})) ==
371+
@test repr(typemin(FixedDecimal{Int32, 2})) ==
367372
"FixedDecimal{Int32,2}(-21474836.48)"
368-
@test string(typemax(FixedDecimal{Int32, 2})) ==
373+
@test repr(typemax(FixedDecimal{Int32, 2})) ==
369374
"FixedDecimal{Int32,2}(21474836.47)"
370375
end
371376
end
377+
378+
@testset "string" begin
379+
for x in keyvalues[FD2]
380+
if 0 abs(x) < 1000
381+
@test eval(parse(string(x))) == x
382+
end
383+
end
384+
end

0 commit comments

Comments
 (0)