Mitchel Verschoof
Mitchel Verschoof

Reputation: 1508

Jquery / Javascript - Math with long numbers

I have a few numbers form my view:

var total      = '100.000.559,99';
var paymentOne = '560';
var paymentTwo = '99.999.999,99';

I change my paymentOne, so I want to recount them. I wrap all the numbers in: parseFloat, but my 100.000.559,99 will be 100. So that wont works..

So my question is, how can I use them for math?

Upvotes: 0

Views: 231

Answers (3)

Bruno
Bruno

Reputation: 5822

Before parsing you could do the following

var total = '100.000.559,99';
total = total.replace( /\./g, "" ).replace( /,/g, "." );

Probably better off wrapping it in a function

function convertToFloat( num ) {
    num = num.replace( /\./g, "" ).replace( /,/g, "." );
    return parseFloat( num );
}

Upvotes: 1

VisioN
VisioN

Reputation: 145398

You may do the conversion to floats with something like that:

+total.split(".").join("").replace(",", ".");  // 100000559.99

Or with regex:

+total.replace(/[.,]/g, function(c) { return c === "," ? "." : "" });

Upvotes: 3

recursive
recursive

Reputation: 86084

It looks like you're using commas instead of periods, and periods where you don't need them at all. Try this:

var total      = 100000559.99;
var paymentOne = 560;
var paymentTwo = 99999999.99;

Upvotes: 1

Related Questions