Skip to content

Commit b490f74

Browse files
committed
Merge pull request #14348 from mason-bially/issue14331
Fixing original issue #14331 by cleaning up print_fixed_point
2 parents bedd4db + e51bfd7 commit b490f74

File tree

2 files changed

+22
-9
lines changed

2 files changed

+22
-9
lines changed

base/printf.jl

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,9 @@ function print_fixed(out, precision, pt, ndigits, trailingzeros=true)
200200
write(out, '0')
201201
ndigits += 1
202202
end
203-
write(out, trailingzeros ? '.' : ' ')
203+
if trailingzeros
204+
write(out, '.')
205+
end
204206
else # 0 < pt < ndigits
205207
# dd.dd0000
206208
ndigits -= pt
@@ -411,7 +413,7 @@ function gen_e(flags::ASCIIString, width::Int, precision::Int, c::Char, inside_g
411413
blk = ifblk.args[2]
412414
push!(blk.args, :((len, pt, neg) = args))
413415
push!(blk.args, :(exp = pt-1))
414-
expmark = c=='E' ? "E" : "e"
416+
expmark = isupper(c) ? "E" : "e"
415417
if precision==0 && '#' in flags
416418
expmark = string(".",expmark)
417419
end
@@ -680,6 +682,17 @@ function gen_p(flags::ASCIIString, width::Int, precision::Int, c::Char)
680682
end
681683

682684
function gen_g(flags::ASCIIString, width::Int, precision::Int, c::Char)
685+
# print to fixed trailing precision
686+
# [g]: lower case e on scientific
687+
# [G]: Upper case e on scientific
688+
#
689+
# flags
690+
# (#): always print a decimal point
691+
# (0): pad left with zeros
692+
# (-): left justify
693+
# ( ): precede non-negative values with " "
694+
# (+): precede non-negative values with "+"
695+
#
683696
x, ex, blk = special_handler(flags,width)
684697
if precision < 0; precision = 6; end
685698
ndigits = min(precision+1,length(DIGITS)-1)
@@ -709,14 +722,9 @@ function gen_g(flags::ASCIIString, width::Int, precision::Int, c::Char)
709722
push!(blk.args, :(width = $width))
710723
# need to compute value before left-padding since trailing zeros are elided
711724
push!(blk.args, :(tmpout = IOBuffer()))
712-
push!(blk.args, :(if fprec > 0
713-
print_fixed(tmpout,fprec,pt,len,$('#' in flags))
714-
else
715-
write(tmpout, pointer(DIGITS), len)
716-
while pt >= (len+=1) write(tmpout,'0') end
717-
end))
725+
push!(blk.args, :(print_fixed(tmpout,fprec,pt,len,$('#' in flags))))
718726
push!(blk.args, :(tmpstr = takebuf_string(tmpout)))
719-
push!(blk.args, :(if fprec > 0 width -= length(tmpstr); end ))
727+
push!(blk.args, :(width -= length(tmpstr)))
720728
if '+' in flags || ' ' in flags
721729
push!(blk.args, :(width -= 1))
722730
else

test/printf.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,11 @@ end
7373
@test( @sprintf( "%.6g", big"12340000.0" ) == "1.234e+07")
7474
@test( @sprintf( "%#.6g", big"12340000.0") == "1.23400e+07")
7575

76+
# %g regression gh #14331
77+
@test( @sprintf( "%.5g", 42) == "42")
78+
@test( @sprintf( "%#.2g", 42) == "42.")
79+
@test( @sprintf( "%#.5g", 42) == "42.000")
80+
7681
# hex float
7782
@test (@sprintf "%a" 1.5) == "0x1.8p+0"
7883
@test (@sprintf "%#.0a" 1.5) == "0x2.p+0"

0 commit comments

Comments
 (0)