Repository navigation
PrecisionNumberTag
Formats a number with configurable decimal precision, falling back to a dash placeholder for empty or invalid values.
Throughout the application, numeric values such as weights, quantities, and monetary amounts need to be displayed with consistent decimal formatting. Rather than repeating Number.toFixed(precision) calls and null/undefined guards in every component, this tag encapsulates the formatting logic in a single, testable unit. Invalid or empty inputs gracefully degrade to a dash via EmptyValueTag.
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
value |
number | string | null | undefined |
Yes | — | The number or numeric string to format. Invalid values render a dash. |
precision |
number |
No | 2 |
Number of decimal places passed to Number.toFixed(). May be negative. |
import PrecisionNumberTag from '@/components/core/Tags/PrecisionNumberTag';
<PrecisionNumberTag value={3.14159} />;
<PrecisionNumberTag value={123.456} precision={4} />;
<PrecisionNumberTag value={null} />;- If the value is
null,undefined,'', or whitespace-only → delegates to<EmptyValueTag />. - Otherwise converts the value to
Number(). - If the result is not finite (
NaN,Infinity,-Infinity) → delegates to<EmptyValueTag value={null} />. - Otherwise renders
<span>{parsedNumber.toFixed(precision)}</span>.
Key edge cases:
-
0is treated as a valid number and rendered as"0.00"(default precision). - String inputs like
"42.5"are parsed to numbers before formatting. - Non-numeric strings like
"abc"produce a dash. - Negative precision values are passed directly to
toFixed(e.g.precision={-1}rounds to the nearest ten).
<PrecisionNumberTag value={3.14159} precision={2} />
<!-- Renders: <span>3.14</span> --><PrecisionNumberTag value={NaN} />
<!-- Renders: <span data-testid="empty-value">-</span> -->
<PrecisionNumberTag value="abc" />
<!-- Renders: <span data-testid="empty-value">-</span> -->
<PrecisionNumberTag value={0} precision={3} />
<!-- Renders: <span>0.000</span> -->- The component imports
EmptyValueTagas its fallback renderer. -
precisiondefaults to2— omit for standard decimal display. - The component does not perform locale-aware formatting (no thousand separators).
-
Number.toFixed()may return unexpected results for very large or very small numbers due to floating-point representation. For financial values requiring exact precision, use a dedicated decimal library at the data layer before passing the value to this component. - No support for locale-aware formatting (
,as thousands separator, custom decimal markers).
No cross-references detected.
- EmptyValueTag — the dash placeholder component used as the invalid-value fallback.
This wiki documents NR Waste Plus for users and developers. Verify security and deployment guidance against the current application configuration before applying it elsewhere.
- Report an issue or ask a question in the GitHub repository.
- Follow the Contributing Guidelines to propose an update.
- Read the Code of Conduct.
Collection indexes support human browsing and machine retrieval. Keep both current when adding or moving wiki pages.
NR Waste Plus Wiki | GitHub Repository