0

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?

Mitchel Verschoof
  • 1,543
  • 4
  • 20
  • 38
  • 3
    `100.000.559,99` is not a valid JavaScript number. Use `100000559.99` or search for a library that interprets locale numbers (because it changes depending on the language used) – elclanrs Jan 03 '13 at 22:57
  • See: http://stackoverflow.com/questions/3072307/is-there-a-bignum-library-for-javascript – Roberto Luis Bisbé Jan 03 '13 at 22:58

3 Answers3

3

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 === "," ? "." : "" });
VisioN
  • 143,310
  • 32
  • 282
  • 281
1

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;
recursive
  • 83,943
  • 34
  • 151
  • 241
1

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 );
}
Bruno
  • 5,772
  • 1
  • 26
  • 43