-1
val = "2.";
var num = parseFloat(val).toFixed(2);

I'm using the parseFloat to turn the string into a number. Then I'm using toFixed(2) to make sure 2 zeros will be added after the decimal point. My problem is if val has a comma this gets all screwed up.

Example:

val = "2,234.";
var num = parseFloat(val).toFixed(2);

The output is 2.00.

How can I allow for the 00 to be added and the comma not to be replaced?

Val is a string to start with sorry about the confusion. it should end up outputting 2,234.00 and the first val should output 2.00

Chapsterj
  • 6,483
  • 20
  • 70
  • 124

3 Answers3

0

There is two possible reasons, depending on what your code actually is, both giving the same result.

If the code is exactly as in the question, i.e.:

val = 2,234.;

then you are using the comma operator, which takes two operands, evaluates both, then returns the value of the second one. The assignment of 2 to the variable val is the first operand, and the value 234. is the second operand. The assignment happens, then 234. is evaluated, returned by the comma operator, and discarded. The code is evaluated as:

(val = 2) , (234.);

If it's a string value, i.e.:

val = "2,234.";

then the reason is that the parseFloat function only parses characters as far as it can, and a comma is not part of the number format so it stops there and returns the 2.

Edit:

To parse the string, you have to remove the comma, parse it, and then put the comma back:

var s = parseFloat(val.replace(/,/g,'')).toFixed(2);
while (/\d{4}/.test(s)) {
  s = s.replace(/(\d{3}[,.])/,',$1');
}

Demo: http://jsfiddle.net/Guffa/KHjjh/

Community
  • 1
  • 1
Guffa
  • 687,336
  • 108
  • 737
  • 1,005
  • Good answer, I was typing mine then this one popped up! Assuming it is in fact a string "2,234." you can simply eliminate the commas with this code: `val.split(",").join("")` Or use the `.replace` function, but that requires regex so you'll probably be better off with the easier one. – Ennui May 01 '13 at 19:43
0

I Believe the only way to work around this is replace the comma with an empty string. This here should work

 val = 2,234.;
 var num = parseFloat(val.replace(',', '')).toFixed(2).replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,");

If the val is already a string this should be the most concise way to do it using jQuery

Four_lo
  • 1,150
  • 10
  • 28
0

Assuming you are using strings.

As Guffa explained parseFloat won't work once it hits the comma... So we'll just strip them out...

var val = '2,234.';

val = val.replace(',', '') // remove commas

var num = parseFloat(val).toFixed(2); // 2234.00

Now we need to put the comma back. Thanks to another answer on stack overflow we have:

// http://stackoverflow.com/questions/6392102/add-commas-to-javascript-output
var addCommas = function(nStr) {
  nStr += '';
  x = nStr.split('.');
  x1 = x[0];
  x2 = x.length > 1 ? '.' + x[1] : '';
  var rgx = /(\d+)(\d{3})/;
  while (rgx.test(x1)) {
      x1 = x1.replace(rgx, '$1' + ',' + '$2');
  }
  return x1 + x2;
};

num = addCommas(num); // "2,234.00"
Will
  • 7,225
  • 2
  • 23
  • 14