
Security News
ESLint Adds Official Support for Linting HTML
ESLint now supports HTML linting with 48 new rules, expanding its language plugin system to cover more of the modern web development stack.
decimal-format
Advanced tools
format decimal numbers with a specific pattern
$ npm install decimal-format --save
or use pnpm
$ pnpm add decimal-format --save
import DecimalFormat from 'decimal-format';
const df = new DecimalFormat('#,##0.0#');
df.format(1234.995); // 1,235.0
df.format(1234.956); // 1,234.96
const df1 = new DecimalFormat('#,##0.0#%');
df1.format(0.34995); // 35.0%
const df2 = new DecimalFormat('#,##0.0#‰');
df2.format(0.034993); // 34.99‰
const df3 = new DecimalFormat('¥#,##0.00元');
df3.format(1234.995); // ¥1,235.00元
RoundingMode defines the rounding modes of the formatting
import DecimalFormat, { RoundingMode } from 'decimal-format';
const df = new DecimalFormat('#,##0.0#');
df.setRoundingMode(RoundingMode.UP); // round away from zero
df.format(2.1) // 3.0
df.format(-2.1) // -3.0
df.setRoundingMode(RoundingMode.UNNECESSARY) // assert that no rounding is necessary
df.format(2.11) // 2.11
df.format(2.123) // throw exception
RoundingMode
Enum | Description |
---|---|
UP | round away from zero |
DOWN | round towards zero |
CEILING | round towards positive infinity |
FLOOR | round towards negative infinity |
HALF_UP | round towards "nearest neighbor" unless both neighbors are equidistant, in which case round up |
HALF_DOWN | round towards "nearest neighbor" unless both neighbors are equidistant, in which case round down |
HALF_EVEN | round towards the "nearest neighbor" unless both neighbors are equidistant, in which case round toward the even neighbor |
UNNECESSARY | assert that the requested operation has an exact result, hence no rounding is necessary |
FAQs
format decimal
The npm package decimal-format receives a total of 4,880 weekly downloads. As such, decimal-format popularity was classified as popular.
We found that decimal-format demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
ESLint now supports HTML linting with 48 new rules, expanding its language plugin system to cover more of the modern web development stack.
Security News
CISA is discontinuing official RSS support for KEV and cybersecurity alerts, shifting updates to email and social media, disrupting automation workflows.
Security News
The MCP community is launching an official registry to standardize AI tool discovery and let agents dynamically find and install MCP servers.