# core/formatters/metric.formatter.ts

> 25 lines of code and 5 definitions.

Tree: GovLab Stats
Language: typescript
Layer: processing
Canonical: https://banes-lab.com/anatomy/stats#file-stats-core-formatters-metric-formatter-ts
Source text: https://banes-lab.com/source/stats/core/formatters/metric.formatter.ts.txt

Listed in [core/formatters](https://banes-lab.com/api/source/stats/core/formatters.md).

## Definitions

- `num` (lexical_declaration, line 19, exported)
- `pct` (lexical_declaration, line 23, exported)
- `humanBytes` (lexical_declaration, line 9, exported)
- `value` (lexical_declaration, line 10, exported)
- `unit` (lexical_declaration, line 11, exported)

## Contained in

- [core/formatters](https://banes-lab.com/anatomy/stats/folder-stats-core-formatters.md)

## Used by

- [core/reporters/dependency.reporter.ts](https://banes-lab.com/source/stats/core/reporters/dependency.reporter.ts.md)
- [core/reporters/metric.reporter.ts](https://banes-lab.com/source/stats/core/reporters/metric.reporter.ts.md)
- [core/reporters/report.reporter.ts](https://banes-lab.com/source/stats/core/reporters/report.reporter.ts.md)
- [core/reporters/site.reporter.ts](https://banes-lab.com/source/stats/core/reporters/site.reporter.ts.md)
- [core/reporters/taxonomy.reporter.ts](https://banes-lab.com/source/stats/core/reporters/taxonomy.reporter.ts.md)
- [core/reporters/validation.reporter.ts](https://banes-lab.com/source/stats/core/reporters/validation.reporter.ts.md)

## Linked from

- [core/reporters](https://banes-lab.com/anatomy/stats/folder-stats-core-reporters.md)

## Source

```typescript
import {
    BYTES_PER_UNIT,
    BYTE_UNITS,
    NUMBER_LOCALE,
    ONE_DECIMAL,
    PERCENT,
} from "#configuration/constants/metric.constants";

export const humanBytes = function humanBytes(bytes: number): string {
    let value = bytes;
    let unit = 0;
    while (value >= BYTES_PER_UNIT && unit < BYTE_UNITS.length - 1) {
        value /= BYTES_PER_UNIT;
        unit += 1;
    }
    return `${value.toFixed(unit === 0 ? 0 : ONE_DECIMAL)} ${BYTE_UNITS.at(unit) ?? ""}`;
};

export const num = function num(value: number): string {
    return value.toLocaleString(NUMBER_LOCALE);
};

export const pct = function pct(part: number, whole: number): string {
    if (whole <= 0) {
        return `${(0).toFixed(ONE_DECIMAL)}%`;
    }
    return `${((part / whole) * PERCENT).toFixed(ONE_DECIMAL)}%`;
};
```
