Given both that the question which created the numeraljs directly links to the Numeral.js site, and that 5 of the 9 questions tagged numeraljs were also tagged numeral.js, I went ahead and merged the two tags with the .js version as the parent tag.
I also created a synonym directing from numeraljs to numeral.js which will prevent someone from creating a dot-less version in future (since it already exists).
If someone is knowledgeable and so inclined to create a better tag wiki excerpt which provides actual usage guidance and a tag wiki which doesn't plagiarise the documentation that'd probably be of value as well.
Answer from Henry Ecker on Stack OverflowVideos
» npm install numeral
» npm install @elastic/numeral
» npm install numeraljs
At lines 67 and 68 of the un–minified code there is:
// figure out what kind of format we are dealing with
if (format.indexOf('$') > -1) { // currency!!!!!
So yes, it seems "$" is the only currency symbol recognised. You can add the currency symbol separately, e.g.:
var amount = '£' + numeral(2456.01231).format('0,0[.]00');
console.log(amount); // £2,456.01
or extend the library to deal with other currency symbols.
It may be better to use the ISO symbol GBP, which is not ambiguous. There are many currencies that use the £ symbol, as there are many that use $.
Import the locale and then set it manually.
import numeral from 'numeral';
import 'numeral/locales/en-gb';
numeral.locale('en-gb');
numeral('1234.56').format('$0,0.00'); // £1,234.56
In this case you will need to use a different format string depending on the size of the original number. It looks like you have the following cases:
- If the number is under 1000, return the same number in long format but with at most two decimal places.
- If the number is between 1000-10,000, return the same number in long format with no decimal places.
- If the number is between 10,000-1,000,000, return the same number in shorthand format, with at most one decimal place.
- If the number is larger than 1,000,000, return the same number in shorthand format, with at most two decimal places.
For each of these cases you will need a different format string. You can do that by comparing the number to each threshold and setting the format string depending on its size.
function formatValue(value, shorthandFormat = true, shorthandLength = 5) {
if (value === undefined || value === null) return '?';
let valueString = value.toString();
if (valueString.trim() === '') return '';
let numeralValue = numeral(value)
// Note the format changes depending on how large the value is.
let formatter = "0[.][00]" // Default formatter: returns the same number with at most, two decimals.
if (value > 1000) formatter = "0" // Over 1000: remove any decimal part.
if (value > 10000) formatter = "0.[0]a" // Over 10000: use shorthand with at most, one decimal.
if (value > 1000000) formatter = "0.[00]a" // Over 1000000: use shorthand with at most, two decimals.
let formattedValue = numeralValue.format(formatter);
return formattedValue;
}
const tests = [
// Small numbers contain at most, two decimals.
[0, "0"],
[1, "1"],
[1.1, "1.1"],
[1.11, "1.11"],
[1.115, "1.12"],
[11.1, "11.1"],
[11.11, "11.11"],
[16.75, "16.75"],
[675, "675"],
[675.5, "675.5"],
[675.55, "675.55"],
// Numbers between one-thousand and ten-thousand may not contain decimals.
[6700.05, "6700"],
[2522.25, "2522"],
[6000, "6000"],
// Numbers between ten-thousand and one-million should use shorthand, with at most one decimal.
[67500.89, "67.5k"],
[67530.85, "67.5k"],
[210500, "210.5k"],
[210000, "210k"],
// Numbers greater than one-million should use shortand, with at most two decimals.
[2120000, "2.12m"],
[6750000, "6.75m"],
[2000000, "2m"],
[2100000, "2.1m"],
[2010000, "2.01m"],
[2001000, "2m"],
]
tests.forEach(([input, output]) => console.log(`${formatValue(input) === output ? "PASS" : "FAIL"}: ${input} --> ${formatValue(input)}. Expected: ${output}`));
<script src="https://cdnjs.cloudflare.com/ajax/libs/numeral.js/2.0.6/numeral.min.js"></script>
Note: The test for number 67500.89 was changed to expect 67.5k as the result, since 6.7k seemed to be a typo.
Here is code example from a number shorthand formatter I built awhile ago that does exactly what you want.
function numberIntoString({
value,
decimals,
}) {
value = value.toString().replace(/[^0-9.]/g, "");
let si = [
{ value: 1e3, symbol: "K" },
{ value: 1e6, symbol: "M" },
{ value: 1e9, symbol: "B" },
{ value: 1e12, symbol: "T" },
];
let i;
for (i = si.length - 1; i > 0; i--) {
if (value >= si[i].value) {
break;
}
}
return (
(value / si[i].value)
.toFixed(decimals)
.replace(/\.0+$|(\.[0-9]*[1-9])0+$/, "$1") + si[i].symbol
);
}