-4

How can I format thousands with dot and decimal with comma?

Input: 3197769.7000000007

Expected: 3.197.769,7000000007

I tried the following regex, but it doesn't feel clean enough. I feel like answer should be similar to How to format a number with commas as thousands separators?, but I don't have enough regex knowledge for it.

const value = 3197769.7000000007
console.log(value.toString().replace(/\B(?<!\.\d*)(?=(\d{3})+(?!\d))/g, "#").replace('.', ',').replace(/#/g, '.'));
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Shinjo
  • 677
  • 6
  • 22
  • @adiga for those marking as duplicate. please notice regex tag – Shinjo Dec 06 '22 at 09:35
  • 1
    It is also marked with javascript and there are native ways to do this than complicated regex – adiga Dec 06 '22 at 09:36
  • 1
    @Shinjo Not everything needs to be solved with regular expressions. And not everything _should_ or even _can_ be solved with regular expressions. – Sebastian Simon Dec 06 '22 at 09:36
  • 1
    In most cases one would use the formatting of [`Intl.NumberFormat`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat/NumberFormat) ... `console.log(new Intl.NumberFormat('de-DE', { maximumFractionDigits: 10 }).format('3197769.7000000007'));` – Peter Seliger Dec 06 '22 at 10:37
  • 2
    You have an [xy problem](https://xyproblem.info/) :). A regex is the wrong tool for formatting a number. – AD7six Dec 06 '22 at 11:08
  • I agree that regex is not the best suited tool since the [Internationalization Web API](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl) offers safer and more convenient solutions. Anyhow what I see from the OP's used regex and also from the linked thread, I have to say that there are much cleaner and more straightforward regex and `replace` based solutions like e.g. this one ... [`String(3197769.7000000007).replace('.', ',').replace(/\d(?=(?:(?:\d{3})+),)/g, match => \`${ match }.\`);`](https://regex101.com/r/3pvKHl/1) – Peter Seliger Dec 06 '22 at 11:41

1 Answers1

2

I agree that regex is not the best suited tool since the Internationalization Web API offers safer and more convenient solutions. Anyhow what I see from the OP's used regex and also from the linked thread, I have to say that there are much cleaner and more straightforward regex and replace based solutions like e.g. this one ...

console.log(
  '3197769.7000000007 =>',

  String(3197769.7000000007)

    // - replace the standard decimal separator by a comma.
    .replace('.', ',')

    // - see ... [https://regex101.com/r/3pvKHl/1]
    // - replace every digit which is followed
    //   by a sequence of 3-digit groups/blocks
    //   until the comma by the very digit itself
    //   and the thousands block separating dot.
    .replace(/\d(?=(?:(?:\d{3})+),)/g, match => `${ match }.`)
);
console.log(
  '6291563197769.587 =>',

  String(6291563197769.587)
    .replace('.', ',')
    .replace(/\d(?=(?:(?:\d{3})+),)/g, match => `${ match }.`)
);
.as-console-wrapper { min-height: 100%!important; top: 0; }

... where one would

  • e.g. cast the number into a string value via the String function,

  • replace the sole occurrence of the decimal delimiter (dot) by a comma,

  • replace every digit which is followed by a sequence of 3-digit groups/blocks until the comma by the very digit itself and the thousands block separating dot ...

Peter Seliger
  • 11,747
  • 3
  • 28
  • 37
  • Thanks, it works fine for the most part, except when it doesn't have a decimal, it fails to [separate thousands](https://i.stack.imgur.com/rbZjd.png). – Shinjo Dec 07 '22 at 02:48