Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Python % format spec #76

Merged
merged 1 commit into from
Feb 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ keywords = ["Strings", "Formatting"]
license = "MIT"
name = "Format"
uuid = "1fa38f19-a742-5d3f-a2b9-30dd87b9d5f8"
version = "1.3.5"
version = "1.3.6"

[deps]

Expand Down
9 changes: 8 additions & 1 deletion src/fmtcore.jl
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,9 @@ end
### print floating point numbers

function _pfmt_f(out::IO, fs::FormatSpec, x::AbstractFloat)
# Handle %
percentflag = (fs.typ == '%')
percentflag && (x *= 100)
# separate sign, integer, and decimal part
prec = fs.prec
rax = round(abs(x); digits = prec)
Expand All @@ -200,7 +203,7 @@ function _pfmt_f(out::IO, fs::FormatSpec, x::AbstractFloat)
# calculate length
xlen = ndigits(intv)
numsep, numini = fs.tsep ? divrem(xlen - 1, 3) : (0, 0)
xlen += ifelse(prec > 0, prec + 1, 0) + (sch != '\0') + numsep
xlen += ifelse(prec > 0, prec + 1, 0) + (sch != '\0') + numsep + percentflag

# calculate padding needed
pad = fs.width - xlen
Expand Down Expand Up @@ -237,6 +240,10 @@ function _pfmt_f(out::IO, fs::FormatSpec, x::AbstractFloat)
nd < prec && _repprint(out, '0', prec - nd)
_outint(out, idecv)
end

# print trailing percent sign
percentflag && print(out, '%')

# right padding
postpad == 0 || _repprint(out, fs.fill, postpad)
end
Expand Down
10 changes: 5 additions & 5 deletions src/fmtspec.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,19 @@
# width ::= <integer>
# prec ::= <integer>
# type ::= 'b' | 'c' | 'd' | 'e' | 'E' | 'f' | 'F' | 'g' | 'G' |
# 'n' | 'o' | 'x' | 'X' | 's'
# 'n' | 'o' | 'x' | 'X' | 's' | '%'
#
# Please refer to http://docs.python.org/2/library/string.html#formatspec
# for more details
#

## FormatSpec type

const _numtypchars = Set(['b', 'd', 'e', 'E', 'f', 'F', 'g', 'G', 'n', 'o', 'x', 'X'])
const _numtypchars = Set(['b', 'd', 'e', 'E', 'f', 'F', 'g', 'G', 'n', 'o', 'x', 'X', '%'])

_tycls(c::AbstractChar) =
(c == 'd' || c == 'n' || c == 'b' || c == 'o' || c == 'x') ? 'i' :
(c == 'e' || c == 'f' || c == 'g') ? 'f' :
(c == 'e' || c == 'f' || c == 'g' || c == '%') ? 'f' :
(c == 'c') ? 'c' :
(c == 's') ? 's' :
error("Invalid type char $(c)")
Expand Down Expand Up @@ -108,7 +108,7 @@ end

## parse FormatSpec from a string

const _spec_regex = r"^(.?[<^>])?([ +-])?(#)?(\d+)?([,_])?(.\d+)?([bcdeEfFgGnosxX])?$"
const _spec_regex = r"^(.?[<^>])?([ +-])?(#)?(\d+)?([,_])?(.\d+)?([bcdeEfFgGnosxX%])?$"

function FormatSpec(s::AbstractString)
# default spec
Expand Down Expand Up @@ -204,7 +204,7 @@ function printfmt(io::IO, fs::FormatSpec, x)
elseif cls == 'f'
fx = float(x)
if isfinite(fx)
ty == 'f' || ty == 'F' ? _pfmt_f(io, fs, fx) :
ty == 'f' || ty == 'F' || ty == '%' ? _pfmt_f(io, fs, fx) :
ty == 'e' || ty == 'E' ? _pfmt_e(io, fs, fx) : _pfmt_g(io, fs, fx)
else
_pfmt_specialf(io, fs, fx)
Expand Down
7 changes: 7 additions & 0 deletions test/fmtspec.jl
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,13 @@ end
@test pyfmt(".1e", 0.0006) == "6.0e-04"
end

@testset "Format percentage (%)" begin
@test pyfmt("8.2%", 0.123456) == " 12.35%"
@test pyfmt("<8.2%", 0.123456) == "12.35% "
@test pyfmt("^8.2%", 0.123456) == " 12.35% "
@test pyfmt(">8.2%", 0.123456) == " 12.35%"
end

@testset "Format special floating point value" begin

@test pyfmt("f", NaN) == "NaN"
Expand Down
Loading