0

I am trying to add 0.10 to 23.50 but I get 23.50.10 instead of 23.60

my code

console.log((parseFloat(23.50) + parseFloat((Math.random() * 0.10) + 0.01).toFixed(2)));

This piece of code generates decimal point like: 0.04, 0.29, etc...

parseFloat((Math.random() * 0.90) + 0.01).toFixed(2)
4
  • 2
    Side note: You don't need (or want) to parseFloat something that's already a number. Commented Apr 13, 2017 at 17:25
  • 2
    toFixed(2) returns a string. Adding that to a number becomes string concatenation. Commented Apr 13, 2017 at 17:25
  • 2
    Do all the arithmetic first, and use toFixed(2) at the end when you want to show it. Commented Apr 13, 2017 at 17:26
  • instead of parseFloat((Math.random() * 0.10) + 0.01) could it be that you mean Math.floor(Math.random() * 10 + 1) / 100 Commented Apr 13, 2017 at 17:38

4 Answers 4

2

Your .toFixed is in the wrong parenthesis.

try

console.log((parseFloat(23.50) + parseFloat((Math.random() * 0.10) + 0.01)).toFixed(2));
Sign up to request clarification or add additional context in comments.

2 Comments

if I want to ask a question for which I need suggestion. How to ask in stackoverflow?
You may wish to look at the Help Section to see if the question is on topic for this site, otherwise I would advise finding the appropriate stackexchange site. ref
2

You have .toFixed(2) in the wrong place. The result of that creates a string which is then being concatenated. It should look like this:

console.log((parseFloat(23.50) + parseFloat((Math.random() * 0.10) + 0.01)).toFixed(2));

Comments

1

Consolidating the comments:

As Scott Sauyet said, toFixed is in the wrong place. You end up with number + string, which converts the number to string and does string concatenation.

As I said: That's not the only problem. You don't parseFloat something that's already a number. Do so converts the number to string, then parses that string as a number, which is at best pointless. :-)

As Barmar said: Do the calculation, then use toFixed on the result:

console.log((23.50 + (Math.random() * 0.10) + 0.01).toFixed(2));

Also, + is lower precedence than *, so you don't need those inner parens (or the last 0 on 0.10):

console.log((23.50 + Math.random() * 0.1 + 0.01).toFixed(2));

...though of course, you could include them for style reasons if you liked.

Comments

1

It may be a typo but your .toFixed() is in the wrong spot. It's translating yourparseFloat((Math.random() * 0.10) + 0.01) to string before the addition to parseFloat(23.50) resulting in string concatenation.

Perform the add and then .toFixed():

console.log((parseFloat(23.50) + parseFloat((Math.random() * 0.10) + 0.01)).toFixed(2));

Or be extra cautious and use a var:

var foo = parseFloat(23.50) + parseFloat((Math.random() * 0.10) + 0.01);
console.log(foo.toFixed(2));

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.