# base/format (vanilla.js 0.4.0)

> https://vanillapunch.com/docs/vanilla.js/0.4.0/api/base/format/

Formatting utility.
- Gathers number-string handling that repeats in every game, such as currency / score display.

Example: Display scores, currency and time

```js
import { Format } from "./build/vanilla.js";

console.log(Format.formatNumber(12480));
console.log(Format.formatCompactNumber(5600000));
console.log(Format.formatPaddedNumber(7, 3));
console.log(Format.formatMinutesSeconds(75));
```

## Functions and constants

- `formatNumber(value, locale = "en-US")` (method, since 0.3.0): Returns with thousands separators. (12480 -> "12,480")
  - `value` number: Number to display.
  - `locale` string: Locale. (default "en-US")
  - returns string: A string with thousands separators. (follows toLocaleString)
- `formatNumberWithUnit(value, unitSuffix, locale = "ko-KR")` (method, since 0.3.0): Thousands separators + unit suffix. (1200000, "won" -> "1,200,000won")
  - `value` number: Number to display.
  - `unitSuffix` string: Unit to append.
  - `locale` string: Locale. (default "ko-KR")
  - returns string: A string with thousands separators and the unit.
- `formatCompactNumber(value, fractionDigits = 1)` (method, since 0.3.0): Returns a large number abbreviated. (1234 -> "1.2K", 5600000 -> "5.6M")
  - `value` number: Number to display. (not shortened if its absolute value is under 1000, negatives get a leading -)
  - `fractionDigits` number: Number of decimal places. (default 1, trailing zeros are removed)
  - returns string: String shortened with K, M or B. (moves up to the next unit when rounding reaches 1000; 999999 is "1M")
- `formatPaddedNumber(value, digitCount, minValue = -Infinity, maxValue = Infinity)` (method, since 0.3.0): Returns the value clamped to a range and zero-padded to the digit count. (7-segment counter display, etc.)
  - formatPaddedNumber(7, 3) -> "007", formatPaddedNumber(-5, 3) -> "-05"
  - `value` number: Number to display.
  - `digitCount` number: Total number of digits. (for negatives the - sign counts as one digit)
  - `minValue` number: Lower bound. (default -Infinity)
  - `maxValue` number: Upper bound. (default Infinity)
  - returns string: A string clamped to the range and padded with leading zeros. (not cut if longer than the digit count)
- `formatMinutesSeconds(totalSeconds)` (method, since 0.3.0): Returns seconds as "min:sec". (75 -> "1:15")
  - `totalSeconds` number: Total seconds. (fractions are dropped, negatives count as 0)
  - returns string: A "minutes:seconds" string. (seconds have two digits, minutes stay as is even past 60)
