Security News
Supply Chain Attack Detected in Solana's web3.js Library
A supply chain attack has been detected in versions 1.95.6 and 1.95.7 of the popular @solana/web3.js library.
zero-decimal-currencies
Advanced tools
Get the smallest currency unit even it is a Zero Decimal Currency
Get the smallest currency unit even it is a Zero Decimal Currency
Get the smallest currency unit even it is a Zero Decimal Currency
This lib will convert the amount to the smallest currency unit (e.g., 100 cents to charge €1.00 or 100 to charge ¥100, a zero-decimal currency).
npm install zero-decimal-currencies
const smallestUnit = require("zero-decimal-currencies");
//or import smallUnit from 'zero-decimal-currencies';
//smallestUnit(amount, currency, display, noRound);
let amount = smallestUnit(100.01, "JPY"); //you don't want to display ¥100.01 to your customer, neither charge 100 times the correct amount
console.log(amount); //'100'
let amount2 = smallestUnit(100.01, "JPY", true); //even display mode set to true, will be nice to zero-decimal currencies
console.log(amount2); //'100'
let amount3 = smallestUnit(100.51, "JPY"); //by default will round up with zero-decimal currencies
console.log(amount3); //'101'
let amount4 = smallestUnit(100.01, "EUR"); //non zero-decimal currency, not useful to display, but useful to charge in Stripe
console.log(amount4); //'10001'
let amount5 = smallestUnit(100.01, "EUR", true); //non zero-decimal currency, useful to display, but not useful to charge in Stripe
console.log(amount5); //'100.01'
let amount6 = smallestUnit(15.7784514, "EUR"); //round is default, using toFixed rules
console.log(amount6); //'1578'
let amount7 = smallestUnit(15.7784514, "EUR", false, true); //the last parameter is a noRound option, that always get the 2 first decimals even a big decimal (that js put in cientific notation)
console.log(amount7); //'1578'
Params | Description |
---|---|
amount (required) | Value that will be converted into the smallest currency unit |
currency (required) | Reference currency to calculate the units |
display (optional) | Whether it should be returned in display format or not. Default: false |
noRound (optional) | Determines if the last digit should be rounded based on the decimals values, even if it is a zero decimal currency. Default: false |
Kelvin Campelo 💬 📖 | Magdiel Campelo 💬 📖 |
Contributions are highly welcome! This repo is commitizen friendly — please read about it here.
FAQs
Get the smallest currency unit even it is a Zero Decimal Currency
We found that zero-decimal-currencies demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers 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
A supply chain attack has been detected in versions 1.95.6 and 1.95.7 of the popular @solana/web3.js library.
Research
Security News
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
Security News
Research
Socket researchers have discovered malicious npm packages targeting crypto developers, stealing credentials and wallet data using spyware delivered through typosquats of popular cryptographic libraries.