You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This package depends on Julia of version 0.2 or above. It has no other dependencies. The package is MIT-licensed.
24
24
25
25
26
-
## Types and Functions
26
+
## Python-style Types and Functions
27
27
28
28
#### Types to Represent Formats
29
29
@@ -106,7 +106,7 @@ One can use ``fmt`` to format a single value into a string, or ``format`` to for
106
106
Format arguments using a format expression given by ``fe``, where``fe`` can be either a string or an instance of ``FormatSpec``.
107
107
108
108
109
-
## Difference from Python's Format
109
+
#### Difference from Python's Format
110
110
111
111
At this point, this package implements a subset of Python's formatting language (with slight modification). Here is a summary of the differences:
112
112
@@ -118,3 +118,85 @@ At this point, this package implements a subset of Python's formatting language
118
118
119
119
- 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.
120
120
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.
0 commit comments