You can test for the decimal separator and remove it and everything thereafter:
// Work out whether decimal separator is . or , for localised numbers
function getDecimalSeparator() {
return /\./.test((1.1).toLocaleString())? '.' : ',';
}
// Round n to an integer and present
function myToLocaleInteger(n) {
var re = new RegExp( '\\' + getDecimalSeparator() + '\\d+$');
return Math.round(n).toLocaleString().replace(re,'');
}
// Test with a number that has decimal places
var n = 12345.99
console.log(n.toLocaleString() + ' : ' + myToLocaleInteger(n)); // 12,345.99 : 12,346
You'll need to change system settings to test thoroughly.
Edit
If you want to change the built–in toLocaleString, try:
// Only modify if toLocaleString adds decimal places
if (/\D/.test((1).toLocaleString())) {
Number.prototype.toLocaleString = (function() {
// Store built-in toLocaleString
var _toLocale = Number.prototype.toLocaleString;
// Work out the decimal separator
var _sep = /\./.test((1.1).toLocaleString())? '.' : ',';
// Regular expression to trim decimal places
var re = new RegExp( '\\' + _sep + '\\d+$');
return function() {
// If number is an integer, call built–in function and trim decimal places
// if they're added
if (parseInt(this) == this) {
return _toLocale.call(this).replace(re,'');
}
// Otherwise, just convert to locale
return _toLocale.call(this);
}
}());
}
This will modify the built–in toLocaleString only if it adds decimal places to integers.