Skip to content

Commit

Permalink
formatPrefix takes a value, not a prefix.
Browse files Browse the repository at this point in the history
  • Loading branch information
mbostock committed Jun 15, 2015
1 parent 66dfb00 commit ac9324c
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 15 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,9 @@ The available *type* values are:

The type `n` is also supported as shorthand for `,g`.

<a name="locale_formatPrefix" href="#locale_formatPrefix">#</a> <i>locale</i>.<b>formatPrefix</b>(<i>specifier</i>, <i>prefix</i>)
<a name="locale_formatPrefix" href="#locale_formatPrefix">#</a> <i>locale</i>.<b>formatPrefix</b>(<i>specifier</i>, <i>value</i>)

Equivalent to [*locale*.format](#locale_format), except the returned function will convert values to the units of the specified SI *prefix* before formatting in fixed point notation. The following prefixes are supported:
Equivalent to [*locale*.format](#locale_format), except the returned function will convert values to the units of the appropriate [SI prefix](https://en.wikipedia.org/wiki/Metric_prefix#List_of_SI_prefixes) for the specified numeric *value* before formatting in fixed point notation. The following prefixes are supported:

* `y` - yocto, 10⁻²⁴
* `z` - zepto, 10⁻²¹
Expand All @@ -144,10 +144,10 @@ Equivalent to [*locale*.format](#locale_format), except the returned function wi
* `Z` - zetta, 10²¹
* `Y` - yotta, 10²⁴

Unlike [*locale*.format](#locale_format) with the `s` format type, this method allows you to specify the SI *prefix* explicitly, rather than computing it dynamically based on the formatted number. In addition, the *precision* for the given *specifier* represents the number of digits past the decimal point (as with `f` fixed point notation), not the number of significant digits. For example:
Unlike [*locale*.format](#locale_format) with the `s` format type, this method returns a formatter with a consistent SI prefix, rather than computing the prefix dynamically for each number. In addition, the *precision* for the given *specifier* represents the number of digits past the decimal point (as with `f` fixed point notation), not the number of significant digits. For example:

```js
var f = formatPrefix(",.0", "µ");
var f = formatPrefix(",.0", 1e-6);
f(.00042); // "420µ"
f(.0042); // "4,200µ"
```
Expand Down
7 changes: 4 additions & 3 deletions src/localeFormat.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,11 @@ export default function(locale) {
};
}

function formatPrefix(specifier, prefix) {
function formatPrefix(specifier, value) {
var f = format((specifier = formatSpecifier(specifier), specifier.type = "f", specifier)),
i = prefixes.indexOf(prefix),
k = Math.pow(10, (i < 0 ? (prefix = "", 0) : 8 - i) * 3);
i = Math.max(-8, Math.min(8, Math.floor((Math.log(value) / (Math.LN10 * 3) + 1e-12)))),
k = Math.pow(10, -i * 3),
prefix = prefixes[8 + i];
return function(value) {
return f(k * value) + prefix;
};
Expand Down
21 changes: 13 additions & 8 deletions test/formatPrefix-test.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
var tape = require("tape"),
format = require("../");

tape("formatPrefix(\"s\", prefix)(number) formats with the specified SI prefix", function(test) {
test.equal(format.formatPrefix(",.0s", "µ")(.00042), "420µ");
test.equal(format.formatPrefix(",.0s", "µ")(.0042), "4,200µ");
test.equal(format.formatPrefix(",.3s", "m")(.00042), "0.420m");
tape("formatPrefix(\"s\", value)(number) formats with the SI prefix appropriate to the specified value", function(test) {
test.equal(format.formatPrefix(",.0s", 1e-6)(.00042), "420µ");
test.equal(format.formatPrefix(",.0s", 1e-6)(.0042), "4,200µ");
test.equal(format.formatPrefix(",.3s", 1e-3)(.00042), "0.420m");
test.end();
});

tape("formatPrefix(\"s\", prefix)(number) treats unknown SI prefix as the empty string", function(test) {
test.equal(format.formatPrefix(",.0s", "F")(42), "42");
tape("formatPrefix(\"s\", value)(number) uses yocto for very small reference values", function(test) {
test.equal(format.formatPrefix(",.0s", 1e-27)(1e-24), "1y");
test.end();
});

tape("formatPrefix(\"$,s\", prefix)(number) formats with the specified SI prefix", function(test) {
var f = format.formatPrefix(" $12,.1s", "M");
tape("formatPrefix(\"s\", value)(number) uses yotta for very small reference values", function(test) {
test.equal(format.formatPrefix(",.0s", 1e27)(1e24), "1Y");
test.end();
});

tape("formatPrefix(\"$,s\", value)(number) formats with the specified SI prefix", function(test) {
var f = format.formatPrefix(" $12,.1s", 1e6);
test.equal(f(-42e6), " -$42.0M");
test.equal(f(+4.2e6), " $4.2M");
test.end();
Expand Down

0 comments on commit ac9324c

Please sign in to comment.