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
Copy file name to clipboardExpand all lines: packages/use-i18n/README.md
+122Lines changed: 122 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -159,3 +159,125 @@ const App = () => {
159
159
)
160
160
}
161
161
```
162
+
163
+
### formatUnit
164
+
165
+
This hook also exposes a `formatUnit` function which can be used to format bits/bytes until [ECMA-402 Unit Preferences](https://github.com/tc39/proposal-smart-unit-preferences) is standardised
166
+
167
+
We follow the IEC standard (base 10) with SI units (kilo,mega,giga,...) [more info here](https://en.wikipedia.org/wiki/Binary_prefix)
168
+
169
+
It accepts an `options` as second parameter:
170
+
- `unit`: Manadatory (see below)
171
+
- `maximumFractionDigits`: The maximum number of fraction digits to use
172
+
- `minimumFractionDigits`: The minimum number of fraction digits to use
173
+
- `short`: if it should output the short or long form of the unit (think `Kb` vs `kilobits`)
174
+
175
+
176
+
```js
177
+
import { useI18n } from'@scaleway/use-i18n'
178
+
import { DateInput } from'@scaleway/ui'
179
+
180
+
constApp= () => {
181
+
const { formatUnit } =useI18n()
182
+
183
+
constunits= [
184
+
formatUnit(12 { unit:'kilobyte' }) // "12 KB" or "12 Ko" in fr an ro
185
+
formatUnit(10**8 { unit:'bytes-humanized' }) // "100 MB" or "100 Mo" in fr an ro
- `formatUnit(12, { unit:'byte' })` => `"12 B"` or `"12 o"` (in fr/ro)
207
+
- `(byte|bit)s-humanized`: This is an automated unit which will print a human readable value
208
+
- `formatUnit(1234567, { unit:'bytes-humanized' })` => `"1.23 MB"` or `"1.23 Mo"` (in fr/ro)
209
+
- `(kilo|mega|giga|tera|peta|exa|zetta|yotta)(bit|byte)(byte|bit)-humanized`: This is also an automated unit which will print a human readable value but in the unit specified
210
+
- `formatUnit(123456789, { unit:'gigabyte-humanized' })` => `"0.12 GB"` or `"0.12 Go"` (in fr/ro)
211
+
- `formatUnit(123456789, { unit:'kilobyte-humanized' })` => `"123456.78 KB"` or `"123456.78 Ko"` (in fr/ro)
212
+
213
+
There is also a compound variant which can only be used with bits:
0 commit comments