Skip to content

Commit 077ff80

Browse files
authored
Merge pull request #49 from jmkuhn/exponent99
exponent > 99
2 parents ac44caf + 46331a3 commit 077ff80

File tree

2 files changed

+20
-15
lines changed

2 files changed

+20
-15
lines changed

src/fmtcore.jl

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -198,17 +198,9 @@ function _pfmt_f(out::IO, fs::FormatSpec, x::AbstractFloat)
198198
end
199199
end
200200

201-
function _pfmt_floate(out::IO, sch::Char, zs::Integer, u::Real, prec::Int, e::Int, ec::Char)
201+
function _pfmt_floate(out::IO, sch::Char, zs::Integer, u::Real, prec::Int, e::Integer, ec::Char)
202202
intv = trunc(Integer,u)
203203
decv = u - intv
204-
if round(Integer, decv * exp10(prec)) == exp10(prec)
205-
intv += 1
206-
if intv == 10
207-
intv = 1
208-
e += 1
209-
end
210-
decv = 0.
211-
end
212204
_pfmt_float(out, sch, zs, intv, decv, prec)
213205
write(out, ec)
214206
if e >= 0
@@ -217,9 +209,10 @@ function _pfmt_floate(out::IO, sch::Char, zs::Integer, u::Real, prec::Int, e::In
217209
write(out, '-')
218210
e = -e
219211
end
220-
(e1, e2) = divrem(e, 10)
221-
write(out, Char('0' + e1))
222-
write(out, Char('0' + e2))
212+
if e < 10
213+
write(out, '0')
214+
end
215+
_pfmt_intdigits(out, e, _Dec())
223216
end
224217

225218

@@ -231,15 +224,19 @@ function _pfmt_e(out::IO, fs::FormatSpec, x::AbstractFloat)
231224
e = 0
232225
u = zero(x)
233226
else
234-
e = floor(Integer,log10(ax)) # exponent
235-
u = ax / exp10(e) # significand
227+
rax = signif(ax, fs.prec + 1)
228+
e = floor(Integer, log10(rax)) # exponent
229+
u = rax / exp10(e) # significand
236230
end
237231

238232
# calculate length
239233
xlen = 6 + fs.prec
234+
if abs(e) > 99
235+
xlen += _ndigits(abs(e), _Dec()) - 2
236+
end
240237
if sch != '\0'
241238
xlen += 1
242-
end
239+
end
243240

244241
# print
245242
ec = isupper(fs.typ) ? 'E' : 'e'

test/fmtspec.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,14 @@ fs = FormatSpec("d")
178178
@test fmt(".1e", 0.6) == "6.0e-01"
179179
@test fmt(".1e", 0.9) == "9.0e-01"
180180

181+
@test fmt("10.2e", 1.2e100) == " 1.20e+100"
182+
@test fmt("11.2e", BigFloat("1.2e1000")) == " 1.20e+1000"
183+
@test fmt("11.2e", BigFloat("1.2e-1000")) == " 1.20e-1000"
184+
@test fmt("9.2e", 9.999e9) == " 1.00e+10"
185+
@test fmt("10.2e", 9.999e99) == " 1.00e+100"
186+
@test fmt("11.2e", BigFloat("9.999e999")) == " 1.00e+1000"
187+
@test fmt("10.2e", -9.999e-100) == " -1.00e-99"
188+
181189
# format special floating point value
182190

183191
@test fmt("f", NaN) == "NaN"

0 commit comments

Comments
 (0)