Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculation, replace dot with a comma

Tags:

jquery

euro

I have an order form on which I use the jQuery Calculation Plugin to sum up the total.

This summing up works fine, yet there is a problem with the produced 'sum'. In the sum I wish to replace any dot with a comma.

The basis of the code is;

function ($this) {
    var sum = $this.sum();
    $("#totaal").html("€ " + sum2);
}

Using a .replace() directly on the var sum doesn't work (referenced function not available on object). I have also tried this (but without effect);

var sum2 = sum.toString().replace(',', '.');

As I'm kind of new to jQuery I'm pretty much stuck now, could anyone point me in the right direction?

like image 975
YDL Avatar asked Mar 13 '11 23:03

YDL


People also ask

How to Replace dot with comma in jquery?

var sum2 = sum. toString(). replace(',', '. ');

How do you change a comma with spaces in a string?

Use the replace() method to replace all commas in a string, e.g. str. replace(/,/g, ' ') . The method takes a regular expression and a replacement string as parameters and returns a new string with the matches replaced by the provided replacement. Copied!


Video Answer


1 Answers

Your replace line is almost right. You need to use a regexp with the g option, which says to replace all instances instead of just the first. You also have the order swapped (first is what to find, second is what to replace it with).

var sum2 = sum.toString().replace(/\./g, ',');

Note the \ before the .: . has a special meaning in a RegExp, so it has to be escaped.

like image 83
Nathan Ostgard Avatar answered Sep 28 '22 09:09

Nathan Ostgard