0

I'm working on a price calculation table on Wordpress using jQuery. I want to space my numbers when the result are in thousands.

Converting this: 1000 into this: 1 000

I tried several jQuery scripts but they all got rid of my .toFixed(2)

var min = 0;
var max = 500000;

    jQuery( "#input" ).keyup(function() {
        if(jQuery("#input").val() < max && jQuery("#input").val() >= min && jQuery("#input").val() < 1000 ) {
            var val = jQuery("#input").val() * jQuery(".price1").val();
            jQuery('#amount1').html(val.toFixed(2));
        }
    });

At the moment the total price is displayed like this: total price: 2349.30$

I'm trying to convert it like this: total price: 2 349.30$

1 Answers1

0

You don't need Jquery you can solve it using regular expressions... https://stackoverflow.com/a/32889998/2894798

var min = 0;
var max = 500000;
var a = 23423;
var b = 309248023;

console.log(max.toFixed(2).toString().replace(/(?!^)(?=(?:\d{3})+(?:\.|$))/gm, ' '))

console.log(a.toFixed(2).toString().replace(/(?!^)(?=(?:\d{3})+(?:\.|$))/gm, ' '))

console.log(b.toFixed(2).toString().replace(/(?!^)(?=(?:\d{3})+(?:\.|$))/gm, ' '))
Renzo Calla
  • 7,486
  • 2
  • 22
  • 37