0

I need to get this (15 321,35) now I have this 15321.35 I have some mistake, and cant find, any ideas?

function money(cislo) {

var cifry = cislo.toString();    
var koma = /\./g, 
 probel = /(\d)(?=(\d{3})+([^\d]|$))/g;

console.log(cifry.replace(koma, ',').replace(probel, '$1 ');
}
var cislo = 15321.35;
money(cislo);

1 Answers1

0

Looks like some syntax errors. You were missing a closing bracket in your console.log();

I've also change the replace function to replace the '.' without regex as it isn't needed. It makes the code overly complex.

function money(cislo) {
  let cifry = cislo.toString();
  let probel = /(\d)(?=(\d{3})+([^\d]|$))/g;

  console.log(cifry.replace('.', ',').replace(probel, '$1 '));
}

var cislo = 15321.35;
money(cislo);
GTBebbo
  • 1,198
  • 1
  • 8
  • 17
  • Thank you:) This code looks better and this missing ) , ohhhhhh:) – Nadia Medvid Apr 16 '20 at 13:02
  • If this helped, mark the answer as correct, thanks! – GTBebbo Apr 16 '20 at 13:03
  • One more question? what if I wanna add the $ at the end of the number? replace(probel, '$0$'), $0 is everything, and $ should add me ''dollar'' at the end! – Nadia Medvid Apr 16 '20 at 13:50
  • If you're just adding the `$` symbol to your string you can just add it to your string: `console.log(cifry.replace('.', ',').replace(probel, '$1 ') + "$");` – GTBebbo Apr 16 '20 at 13:53