If you want to use built-in code, you can use toLocaleString() with minimumFractionDigits.
Browser compatibility for the extended options on toLocaleString() was limited when I first wrote this answer, but the current status looks good. If you're using Node.js, you will need to npm install the intl package.
Use with AIvar value = (100000).toLocaleString(
undefined, // leave undefined to use the visitor's browser
// locale or a string like 'en-US' to override it.
{ minimumFractionDigits: 2 }
);
console.log(value);
Run code snippetEdit code snippet Hide Results Copy to answer Expand
Number formatting varies between cultures. Unless you're doing string comparison on the output,1 the polite thing to do is pick undefined and let the visitor's browser use the formatting they're most familiar with.2
Use with AI// Demonstrate selected international locales
var locales = [
undefined, // Your own browser
'en-US', // United States
'de-DE', // Germany
'ru-RU', // Russia
'hi-IN', // India
'de-CH', // Switzerland
];
var n = 100000;
var opts = { minimumFractionDigits: 2 };
for (var i = 0; i < locales.length; i++) {
console.log(locales[i], n.toLocaleString(locales[i], opts));
}
Run code snippetEdit code snippet Hide Results Copy to answer Expand
If you are from a culture with a different format from those above, please edit this post and add your locale code.
1 Which you shouldn't.
2 Obviously do not use this for currency with something like {style: 'currency', currency: 'JPY'} unless you have converted to the local exchange rate. You don't want your website to tell people the price is ¥300 when it's really $300. Sometimes real e-commerce sites make this mistake.
If you want to use built-in code, you can use toLocaleString() with minimumFractionDigits.
Browser compatibility for the extended options on toLocaleString() was limited when I first wrote this answer, but the current status looks good. If you're using Node.js, you will need to npm install the intl package.
Use with AIvar value = (100000).toLocaleString(
undefined, // leave undefined to use the visitor's browser
// locale or a string like 'en-US' to override it.
{ minimumFractionDigits: 2 }
);
console.log(value);
Run code snippetEdit code snippet Hide Results Copy to answer Expand
Number formatting varies between cultures. Unless you're doing string comparison on the output,1 the polite thing to do is pick undefined and let the visitor's browser use the formatting they're most familiar with.2
Use with AI// Demonstrate selected international locales
var locales = [
undefined, // Your own browser
'en-US', // United States
'de-DE', // Germany
'ru-RU', // Russia
'hi-IN', // India
'de-CH', // Switzerland
];
var n = 100000;
var opts = { minimumFractionDigits: 2 };
for (var i = 0; i < locales.length; i++) {
console.log(locales[i], n.toLocaleString(locales[i], opts));
}
Run code snippetEdit code snippet Hide Results Copy to answer Expand
If you are from a culture with a different format from those above, please edit this post and add your locale code.
1 Which you shouldn't.
2 Obviously do not use this for currency with something like {style: 'currency', currency: 'JPY'} unless you have converted to the local exchange rate. You don't want your website to tell people the price is ¥300 when it's really $300. Sometimes real e-commerce sites make this mistake.
Use
Use with AInum = num.toFixed(2);
Where 2 is the number of decimal places
Edit:
Here's the function to format number as you want
Use with AIfunction formatNumber(number)
{
number = number.toFixed(2) + '';
x = number.split('.');
x1 = x[0];
x2 = x.length > 1 ? '.' + x[1] : '';
var rgx = /(\d+)(\d{3})/;
while (rgx.test(x1)) {
x1 = x1.replace(rgx, '$1' + ',' + '$2');
}
return x1 + x2;
}
Sorce: www.mredkj.com
You can use padStart() to make sure a string is a certain length. If it's not it will pad it with whatever you want. In this case 0:
const nf = (n, c) => n.toString().padStart(c, '0');
function convertSeconds(s) {
var min = Math.floor(s / 60);
var sec = (s % 60)
return nf(min, 2) + ':' + nf(sec, 2);
}
console.log(convertSeconds(64))
console.log(convertSeconds(119))
Run code snippetEdit code snippet Hide Results Copy to answer Expand
Not sure if you want to pad the minutes of not.
There is also the Intl.NumberFormat() which has an option for minimum digits (though this seems like overkill for this):
console.log(new Intl.NumberFormat('en-US', { minimumIntegerDigits: 2}).format(2));
console.log(new Intl.NumberFormat('en-US', { minimumIntegerDigits: 2}).format(59));
Run code snippetEdit code snippet Hide Results Copy to answer Expand
To pad zeroes you can do
function nf(num, places) {
var s = '0' + num;
return s.slice(places * -1);
}
function convertSeconds(s) {
var min = Math.floor(s / 60);
var sec = s % 60;
return nf(min, 2) + ':' + nf(sec, 2);
}
Should get you what you want