Skip to content

Commit 08d46bc

Browse files
committed
Merge pull request #1 from tonyhffong/master
Merging NumFormat into Formatting
2 parents 6751d03 + e47ba2a commit 08d46bc

File tree

8 files changed

+559
-20
lines changed

8 files changed

+559
-20
lines changed

.travis.yml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ before_install:
1212
- sudo add-apt-repository ppa:staticfloat/${JULIAVERSION} -y
1313
- sudo apt-get update -qq -y
1414
- sudo apt-get install libpcre3-dev julia -y
15+
- if [[ -a .git/shallow ]]; then git fetch --unshallow; fi
1516
script:
16-
- julia -e 'Pkg.init(); run(`ln -s $(pwd()) $(Pkg.dir("Formatting"))`); Pkg.pin("Formatting"); Pkg.resolve()'
17-
- julia -e 'using Formatting; @assert isdefined(:Formatting); @assert typeof(Formatting) === Module'
18-
- julia runtests.jl
17+
- julia -e 'versioninfo(); Pkg.init(); Pkg.clone(pwd()); Pkg.test("Formatting")'

README.md

Lines changed: 85 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Formatting
22

3-
A Julia package for Python-like formatting.
3+
This package offers Python-style general formatting and c-style numerical formatting (for speed).
44
[![Build Status](https://travis-ci.org/lindahua/Formatting.jl.png?branch=master)](https://travis-ci.org/lindahua/Formatting.jl)
55

66
---------------
@@ -23,7 +23,7 @@ using Formatting
2323
This package depends on Julia of version 0.2 or above. It has no other dependencies. The package is MIT-licensed.
2424

2525

26-
## Types and Functions
26+
## Python-style Types and Functions
2727

2828
#### Types to Represent Formats
2929

@@ -106,7 +106,7 @@ One can use ``fmt`` to format a single value into a string, or ``format`` to for
106106
Format arguments using a format expression given by ``fe``, where ``fe`` can be either a string or an instance of ``FormatSpec``.
107107

108108

109-
## Difference from Python's Format
109+
#### Difference from Python's Format
110110

111111
At this point, this package implements a subset of Python's formatting language (with slight modification). Here is a summary of the differences:
112112

@@ -118,3 +118,85 @@ At this point, this package implements a subset of Python's formatting language
118118

119119
- The package provides support for filtering (for explicitly positioned arguments), such as ``{1|>lowercase}`` by allowing one to embed the ``|>`` operator, which the Python counter part does not support.
120120

121+
## C-style functions
122+
123+
The c-style part of this package aims to get around the limitation that
124+
`@sprintf` has to take a literal string argument.
125+
The core part is basically a c-style print formatter using the standard
126+
`@sprintf` macro.
127+
It also adds functionalities such as commas separator (thousands), parenthesis for negatives,
128+
stripping trailing zeros, and mixed fractions.
129+
130+
### Usage and Implementation
131+
132+
The idea here is that the package compiles a function only once for each unique
133+
format string within the `Formatting.*` name space, so repeated use is faster.
134+
Unrelated parts of a session using the same format string would reuse the same
135+
function, avoiding redundant compilation. To avoid the proliferation of
136+
functions, we limit the usage to only 1 argument. Practical consideration
137+
would suggest that only dozens of functions would be created in a session, which
138+
seems manageable.
139+
140+
Usage
141+
```julia
142+
using Formatting
143+
144+
fmt = "%10.3f"
145+
s = sprintf1( fmt, 3.14159 ) # usage 1. Quite performant. Easiest to switch to.
146+
147+
fmtrfunc = generate_formatter( fmt ) # usage 2. This bypass repeated lookup of cached function. Most performant.
148+
s = fmtrfunc( 3.14159 )
149+
150+
s = format( 3.14159, precision=3 ) # usage 3. Most flexible, with some non-printf options. Least performant.
151+
```
152+
### Speed
153+
154+
`sprintf1`: Speed penalty is about 20% for floating point and 30% for integers.
155+
156+
If the formatter is stored and used instead (see the example using `generate_formatter` above),
157+
the speed penalty reduces to 10% for floating point and 15% for integers.
158+
159+
### Commas
160+
161+
This package also supplements the lack of thousand separator e.g. `"%'d"`, `"%'f"`, `"%'s"`.
162+
163+
Note: `"%'s"` behavior is that for small enough floating point (but not too small),
164+
thousand separator would be used. If the number needs to be represented by `"%e"`, no
165+
separator is used.
166+
167+
### Flexible `format` function
168+
169+
This package contains a run-time number formatter `format` function, which goes beyond
170+
the standard `sprintf` functionality.
171+
172+
An example:
173+
```julia
174+
s = format( 1234, commas=true ) # 1,234
175+
s = format( -1234, commas=true, parens=true ) # (1,234)
176+
```
177+
178+
The keyword arguments are (Bold keywards are not printf standard)
179+
180+
* width. Integer. Try to fit the output into this many characters. May not be successful.
181+
Sacrifice space first, then commas.
182+
* precision. Integer. How many decimal places.
183+
* leftjustified. Boolean
184+
* zeropadding. Boolean
185+
* commas. Boolean. Thousands-group separator.
186+
* signed. Boolean. Always show +/- sign?
187+
* positivespace. Boolean. Prepend an extra space for positive numbers? (so they align nicely with negative numbers)
188+
* **parens**. Boolean. Use parenthesis instead of "-". e.g. `(1.01)` instead of `-1.01`. Useful in finance. Note that
189+
you cannot use `signed` and `parens` option at the same time.
190+
* **stripzeros**. Boolean. Strip trailing '0' to the right of the decimal (and to the left of 'e', if any ).
191+
* It may strip the decimal point itself if all trailing places are zeros.
192+
* This is true by default if precision is not given, and vice versa.
193+
* alternative. Boolean. See `#` alternative form explanation in standard printf documentation
194+
* conversion. length=1 string. Default is type dependent. It can be one of `aAeEfFoxX`. See standard
195+
printf documentation.
196+
* **mixedfraction**. Boolean. If the number is rational, format it in mixed fraction e.g. `1_1/2` instead of `3/2`
197+
* **mixedfractionsep**. Default `_`
198+
* **fractionsep**. Default `/`
199+
* **fractionwidth**. Integer. Try to pad zeros to the numerator until the fractional part has this width
200+
* **tryden**. Integer. Try to use this denominator instead of a smaller one. No-op if it'd lose precision.
201+
202+
See the test script for more examples.

runtests.jl

Lines changed: 0 additions & 9 deletions
This file was deleted.

src/Formatting.jl

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@ module Formatting
22

33
import Base.show
44

5-
export
6-
FormatSpec, FormatExpr,
7-
printfmt, printfmtln, fmt, format
5+
export
6+
FormatSpec, FormatExpr,
7+
printfmt, printfmtln, fmt, format,
8+
sprintf1, generate_formatter
89

910

11+
include("cformat.jl" )
1012
include("fmtspec.jl")
1113
include("fmtcore.jl")
1214
include("formatexpr.jl")

0 commit comments

Comments
 (0)