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");
1
  • You don't have any string... :-? Commented Jul 1, 2015 at 15:14

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

1 Comment

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
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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.