Last active
October 27, 2022 15:06
-
-
Save stuller/b0a7122d286aa2a7189cc75de561439a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// console.log('US', formatNumber(" #,###.##", 1234.56)) //US | |
// console.log('US', formatNumber(" #,###.##", 12345.67)) //US | |
// console.log('US', formatNumber(" #,###.##", 123412345.67)) //US | |
// console.log('DE', formatNumber(" #.###,##", 1234.56)) //DE | |
// console.log('DE', formatNumber("#.###,##", 12345.67)) //DE | |
// console.log('DE', formatNumber("#.###,##", 1234512345.67)) //DE | |
// console.log('FR', formatNumber('# ###,##', 1234.56)) //FR | |
// console.log('FR', formatNumber('# ###,##', 12345.67)) //FR | |
// console.log('FR', formatNumber('# ###,##', 1234512345.67)) //FR | |
// console.log('CH', formatNumber("#'###.##", 1234.56)) //CH | |
// console.log('CH', formatNumber("#'###.##", 12345.67)) //CH | |
// console.log('CH', formatNumber("#'###.##", 1234512345.67)) //CH | |
// console.log('IN', formatNumber("#,##,###.##", 12345)) //foo | |
// console.log('IN', formatNumber("#,##,###.##", 345.67))//foo | |
// console.log('IN', formatNumber("#,##,###.##", 12345.67)) //foo | |
// console.log('IN', formatNumber("#,##,###.##", 12345.67)) //foo | |
// console.log('IN', formatNumber("#,##,###.##", 1234512345.67)) //foo | |
function formatNumber(pattern, number) { | |
pattern = pattern.trim(); | |
let formattedNumber; | |
const separators = pattern.match(/[^\#]/g); | |
const decimal = separators[separators.length-1]; | |
const groupSeparator = separators[0]; | |
const groupLengths = pattern.split(decimal)[0].split(groupSeparator).slice(1).map(a => a.length); | |
const groupLength = pattern.split(decimal)[0].split(groupSeparator).pop().length; | |
if(groupLengths.length === 1) { | |
const integerRegex = new RegExp(/^[+-]?\d+/) | |
const groupingRegexPattern = `(\\d)(?=(\\d{${groupLength}})+$)`; | |
const groupingRegex = new RegExp(groupingRegexPattern, 'g'); | |
formattedNumber = number.toString().replace('.', decimal).replace(integerRegex, function(int) { | |
return int.replace(groupingRegex, `$1${groupSeparator}`); | |
}); | |
} else { //this is for multiple grouping lengths, like India #,##,###.## | |
const lastGroupLength = groupLengths[groupLengths.length - 1]; | |
const firstGroupLength = groupLengths[0]; | |
const decimals = number.toString().split('.')[1] || ''; | |
const formattedDecimal = decimals ? `${decimal}${decimals}` : ''; | |
let integers = number.toString().split('.')[0]; | |
let lastGroup = integers.substring(integers.length-lastGroupLength); | |
const otherNumbers = integers.substring(0,integers.length-lastGroupLength); | |
const regex = new RegExp(`\\B(?=(\\d{${firstGroupLength}})+(?!\\d))`, 'g'); | |
lastGroup = otherNumbers != '' ? `,${lastGroup}` : lastGroup; | |
const formattedIntegers = otherNumbers.replace(regex, ',') + lastGroup; | |
formattedNumber = `${formattedIntegers}${formattedDecimal}`; | |
} | |
return formattedNumber; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment