A flexible React component for displaying numbers with smart formatting and type-specific styling.
- Smart Number Formatting: Automatically formats numbers based on their magnitude
- Multiple Types: Support for dollar amounts, percentages, and token values
- Very Small Number Handling: Special formatting for very small decimal numbers with subscript notation
- Customizable: Flexible styling with className support
- TypeScript: Full TypeScript support with comprehensive type definitions
- Lightweight: No external UI dependencies, focused purely on number display
npm install numdisplayMake sure you have the following peer dependencies installed:
npm install react react-domimport NumDisplay from "numdisplay";
function App() {
return (
<div>
<NumDisplay value="1234.56" type="dollar" />
<NumDisplay value="15.75" type="percentage" />
<NumDisplay value="0.00001234" type="token" />
</div>
);
}| Prop | Type | Description | Default |
|---|---|---|---|
value |
string |
The numeric value to display | - |
type |
"dollar" | "percentage" | "token" |
The type of number being displayed | - |
decimals |
number |
Fixed number of decimal places (optional) | undefined |
className |
string |
CSS class for styling | - |
showPlusSign |
boolean |
Show plus sign for positive numbers | false |
prefix |
string | ReactNode |
Content to display before the number | - |
suffix |
string | ReactNode |
Content to display after the number | - |
prefixClassName |
string |
CSS class for prefix styling | - |
suffixClassName |
string |
CSS class for suffix styling | - |
Same as NumDisplay but without prefix, suffix, prefixClassName, and suffixClassName.
<NumDisplay value="1234.56" type="dollar" />
// Displays: $1,234.56<NumDisplay value="15.756" type="percentage" decimals={1} />
// Displays: 15.7%<NumDisplay value="0.00001234" type="token" />
// Displays: 0.₄12 (with subscript showing omitted zeros)<NumDisplay
value="100"
type="token"
prefix="Balance:"
suffix="ETH"
showPlusSign={true}
/>
// Displays: Balance: +100 ETH<NumDisplay
value="1000"
type="dollar"
className="text-lg font-bold text-green-600"
prefixClassName="text-gray-500"
prefix="Total:"
/>The component automatically applies different decimal precision based on the number's magnitude:
>= 100: 0 decimals>= 10: 1 decimal>= 1: 2 decimals>= 0.1: 3 decimals>= 0.01: 4 decimals< 0.01: Special very small number formatting
>= 100: 0 decimals>= 10: 1 decimal>= 0.01: 2 decimals< 0.01: Special very small number formatting
>= 100: 0 decimals>= 10: 1 decimal< 10: 2 decimals
For very small numbers (< 0.01), the component uses a special notation:
<NumDisplay value="0.000001234" type="dollar" />
// Displays: $0.12₄
// Where ₄ indicates 4 omitted zeros after the decimal pointThe package includes comprehensive TypeScript definitions:
import NumDisplay, { NumDisplayProps, CoreNumDisplayProps } from "numdisplay";
const MyComponent: React.FC<{ config: NumDisplayProps }> = ({ config }) => {
return <NumDisplay {...config} />;
};The component uses Tailwind CSS classes and can be styled with:
classNamefor the main number displayprefixClassNameandsuffixClassNamefor prefix/suffix styling- The component is designed to work well with Tailwind CSS but doesn't require it
MIT
Contributions are welcome! Please feel free to submit a Pull Request.