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
Add humanize: true option to Localize.Unit.to_string/2, covering bit, byte, hertz and watt bases
The option renders any unit in the unit people expect for its
magnitude: bit-, byte-, hertz- and watt-based units scale through
the humanize/2 prefix ladder (system: :si or :iec, IEC restricted
to bits and bytes), and all other units render through the
usage-based preference pipeline with the struct's usage or
:default. An explicit :usage option keeps precedence. humanize/2
and humanize!/2 gain the hertz and watt bases as well.
Copy file name to clipboardExpand all lines: CHANGELOG.md
+8Lines changed: 8 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,6 +4,14 @@ All notable changes to this project will be documented in this file.
4
4
5
5
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
6
6
7
+
## [Unreleased]
8
+
9
+
### Added
10
+
11
+
*`Localize.Unit.to_string/2` accepts `humanize: true` to render any unit in the unit people expect for its magnitude: bit-, byte-, hertz- and watt-based units scale through the `humanize/2` prefix ladder (honouring `system: :si | :iec`) and all other units render through usage-based preferences. An explicit `:usage` option keeps precedence for preference-scaled units.
12
+
13
+
*`Localize.Unit.humanize/2` and `humanize!/2` also scale hertz- and watt-based units through the SI prefix ladder ("2.5 gigahertz", "3.2 gigawatts") — like bits and bytes, these are locale-invariant quantities that CLDR's unit preferences do not cover. IEC prefixes remain restricted to bit- and byte-based units.
|`:system`|`:si` (default), `:iec`|`:si` scales by 1000 (kilobyte, megabyte, ...); `:iec` by 1024 (kibibyte, mebibyte, ...) |
150
+
|`:humanize`|`true`, `false` (default) | On `to_string/2`: bit/byte/hertz/watt units scale through the prefix ladder, all others use usage-based preferences |
151
+
|`:system`|`:si` (default), `:iec`|`:si` scales by 1000 (kilobyte, megahertz, ...); `:iec` by 1024 (kibibyte, mebibyte, ...) — bits and bytes only |
`Localize.Unit.humanize/2` converts a byte-or bit-based unit to the prefixed unit that best fits its magnitude — the conventional way to display file sizes. It selects the largest prefix such that the converted value is at least 1:
234
+
`Localize.Unit.humanize/2` converts a bit-, byte-, hertz- or watt-based unit to the prefixed unit that best fits its magnitude — the conventional way to display file sizes, frequencies and power figures. It selects the largest prefix such that the converted value is at least 1:
The `:system` option selects the prefix ladder: `:si` (powers of 1000 — kilobyte, megabyte, gigabyte, the default) or `:iec` (powers of 1024 — kibibyte, mebibyte, gibibyte):
262
+
The `:system` option selects the prefix ladder: `:si` (powers of 1000 — kilobyte, megabyte, gigabyte, the default) or `:iec` (powers of 1024 — kibibyte, mebibyte, gibibyte). IEC prefixes apply only to bit- and byte-based units:
Note that CLDR provides display patterns (like `"MB"` in the narrow width) only for SI-prefixed digital units; IEC units format with their full names in all widths. Values below one kilobyte (or kibibyte) are returned unchanged, and already-prefixed units are rescaled from their base value. Non-digital units return an error:
271
+
Note that CLDR provides display patterns (like `"MB"` in the narrow width) only for SI-prefixed units; IEC units format with their full names in all widths. Values below one kilo-unit (or kibi-unit) are returned unchanged, and already-prefixed units are rescaled from their base value. Units with any other base return an error — physical quantities are covered by usage-based preferences instead (see below):
267
272
268
273
```elixir
269
274
iex> {:ok, meters} =Localize.Unit.new(5, "meter")
270
275
iex> {:error, _} =Localize.Unit.humanize(meters)
271
276
```
272
277
278
+
### Humanizing any unit: `humanize: true`
279
+
280
+
The simplest way to render *any* unit in the unit people expect for its magnitude is the `:humanize` option on `to_string/2`:
Underneath, the option dispatches by unit kind. Bit-, byte-, hertz- and watt-based units scale through the `humanize/2` prefix ladder above — the locale-invariant quantities that CLDR's unit-preference data does not cover ("kB", "MHz" and "GW" mean the same thing in every locale). Every other unit is rendered through the usage-based preference pipeline (see [Usage preferences](#usage-preferences) below) with the struct's usage or `:default`. CLDR unit preferences pick the display unit by territory *and* magnitude, which does more than prefix scaling could: grams scale to kilograms and then to tonnes (not to the technically-correct-but-unidiomatic megagram), and a US-locale reader sees miles, tons, feet and inches rather than SI units at all:
An explicit `:usage` option (as above) takes precedence for preference-scaled units, and is the ICU-compatible precise control — ICU triggers the same behaviour with the `usage()` setting on `NumberFormatter`. Use `humanize/2` directly when you want the scaled `%Localize.Unit{}` itself rather than a formatted string.
301
+
273
302
## Measurement system preferences
274
303
275
304
`Localize.Unit.convert_measurement_system/2` converts a unit to the preferred unit for a measurement system. CLDR defines preferences for three systems:
`convert_measurement_system/2` targets `:metric`, `:us`, or `:uk` preferred units (1000 meters → mile for `:us`).
78
78
79
-
## Humanizing digital units (file sizes)
79
+
## Humanizing (auto-scaling to the expected unit)
80
80
81
-
`humanize/2` and `humanize!/2` convert a byte- or bit-based unit to the prefix that best fits its magnitude — the idiomatic way to render file sizes. `system: :si` (powers of 1000, default) or`system: :iec` (powers of 1024); CLDR has compact narrow patterns (`"MB"`) only for SI prefixes, IEC units render with full names. Non-digital units return `InvalidValueError`.
81
+
`to_string(unit, humanize: true)` renders any unit in the unit people expect for its magnitude: bit-, byte-, hertz- and watt-based units scale through the SI prefix ladder (or IEC with`system: :iec`, bits/bytes only), all other units go through usage-based preferences (struct usage or `:default`; an explicit `usage:` wins).
`humanize/2` and `humanize!/2` are the struct-level API for the prefix-scaled bases (bit, byte, hertz, watt) — they convert to the prefixed unit that best fits the magnitude and return the scaled `%Localize.Unit{}`. `system: :si` (powers of 1000, default) or `system: :iec` (powers of 1024, bits/bytes only); CLDR has compact narrow patterns (`"MB"`, `"MHz"`) only for SI prefixes, IEC units render with full names. Other bases return `InvalidValueError`.
0 commit comments