-2

Is there a generic way or regex that handles all possible number formats in string and converts to correct number. For example:

"1.234,21" to 1234.21
"1,234.21" to 1234.21
"1234,21" to 1234.21
"1234.21" to 1234.21
"1,234,567.21" to 1234567.21
"1.234.567,21" to 1234567.21

Edit: Should also handle

"1,234,567" to 1234567
"1.234.567" to 1234567
Zeeshan
  • 1,078
  • 9
  • 14
  • 2
    maybe try converting all commas to periods, then using the answer [here](https://stackoverflow.com/questions/12116586/replace-all-but-last-instance-of-specified-character) to remove all the periods except the last. Finally, use `parseFloat()` to get the final number. – Sir Archibald Humphrey Sep 11 '22 at 18:46
  • Replace all delimiters with a dot, strip out all but the last one? – Bergi Sep 11 '22 at 18:46
  • What if the number is not a decimal number? For example, 1,234,567 won't be covered by this logic. – Zeeshan Sep 12 '22 at 08:07

1 Answers1

0

Solution

A little bit hacky, but works:

const strings = [
"1.234,21",
"1,234.21",
"1234,21",
"1234.21",
"1,234,567.21",
"1.234.567,21",
'1234'
]

const numbers = strings.map(str => {
  const [, int, frac] = str.match(/^(\d+(?:[,.]\d+)*?)+?(?:[,.](\d+))?$/)
  return Number(int.replace(/[,.]/g, '')) + (frac ? Number('0.' + frac) : 0)
})

console.log(numbers)

Explanation

This regex splits the string into two parts by the last , or .

/^(\d+(?:[,.]\d+)*?)+?(?:[,.](\d+))?$/

This converts the integer part to a number:

Number(int.replace(/[,.]/g, ''))

This converts fraction part into a number (if it exists):

frac ? Number('0.' + frac) : 0
Konrad
  • 21,590
  • 4
  • 28
  • 64
  • Thanks for the solution, however, it always considers last , or . as decimal. For example 1,234,567 gets converted 1234.567 and 1.234.567 also gets converted to 1234.567. Any idea how to solve that? – Zeeshan Sep 12 '22 at 08:05
  • I don't see how it should be converted in the other way. A thousand separators aren't usually used for fractions. – Konrad Sep 12 '22 at 09:39
  • In some locales, for example, in German, the comma is used as a decimal separator and the dot is used as a thousand separator. In my case, the text amount can arrive in any format and with a decimal or without a decimal which is why I need to be able to handle that. – Zeeshan Sep 12 '22 at 10:54
  • It's impossible to handle because you will never know. It would be easier if you will get language code or something like that with this data. – Konrad Sep 12 '22 at 11:54
  • 1
    Unfortunately, that's not possible. The data comes from manually written document sources where different users could use different locales and I have no control over the formatting/locales they use. Anyways, thanks for your solution and comments. – Zeeshan Sep 12 '22 at 18:09