Skip to content

Commit 4fd69c4

Browse files
committed
writedlm() modified to print leading zeros when writing floats. For example, 0.5 was written as .5 now after modification writedl() writes as 0.5.
1 parent a116b34 commit 4fd69c4

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

base/grisu.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ Base.showcompact(io::IO, x::Float16) = _show(io, x, PRECISION, 5, false)
127127

128128
# normal:
129129
# 0 < pt < len ####.#### len+1
130-
# pt <= 0 .000######## len-pt+1
130+
# pt <= 0 0.000######## len-pt+1
131131
# len <= pt (dot) ########000. pt+1
132132
# len <= pt (no dot) ########000 pt
133133
# exponential:
@@ -149,8 +149,8 @@ function _print_shortest(io::IO, x::AbstractFloat, dot::Bool, mode, n::Int)
149149
write(io, dec(e))
150150
return
151151
elseif pt <= 0
152-
# => .000########
153-
write(io, '.')
152+
# => 0.000########
153+
write(io, "0.")
154154
while pt < 0
155155
write(io, '0')
156156
pt += 1

test/datafmt.jl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,18 @@ let x = [1,2,3], y = [4,5,6], io = IOBuffer()
9393
@test readcsv(io) == [x y]
9494
end
9595

96+
let x = [0.1 0.3 0.5], io = IOBuffer()
97+
writedlm(io, x, ", ")
98+
seek(io, 0)
99+
@test readall(io) == "0.1, 0.3, 0.5\n"
100+
end
101+
102+
let x = [0.1 0.3 0.5], io = IOBuffer()
103+
writedlm(io, x, ", ")
104+
seek(io, 0)
105+
@test readcsv(io) == [0.1 0.3 0.5]
106+
end
107+
96108
let x = ["abc", "def\"ghi", "jk\nl"], y = [1, ",", "\"quoted\""], io = IOBuffer()
97109
writedlm(io, zip(x,y), ',')
98110
seek(io, 0)

0 commit comments

Comments
 (0)