1

I was wondering how to use parseFloat with a string?. Example:

var N = 3.43;
var M = 43.37;
var subtotal = N + M;
subtotal=parseFloat("subtotal");
Nakayuma
  • 29
  • 4

2 Answers2

2

You don't need parseFloat in your case.

To use parseFloat on a variable:

subtotal = parseFloat(subtotal); // No quotes here

Example:

var value = "3.14"; // String
var PI = parseFloat(value); // Float

OR

var PI = Number(value); // Float

OR

var PI = +value; // Float
Tushar
  • 85,780
  • 21
  • 159
  • 179
  • For your example, I would not use `parseFloat`, but rather `Number` or even `+` : ```var value = "3.14"; var valueAsNumber = Number(value); var valueAsNumber2 = +value; ``` Since `parseFloat` is much less performant. I would keep it to parse "33.333%", though – laruiss Jul 01 '15 at 14:35
  • @laruiss Thanks! Updated answer – Tushar Jul 01 '15 at 14:39
1

Remove the quotes that you are passing inside parsefloat.

var N = 3.43;
var M = 43.37;
var subtotal = N + M;
subtotal=parseFloat(subtotal);
sTg
  • 4,313
  • 16
  • 68
  • 115