1

var sub_total = 0;
  for(var i = 0; i <= 3; i++){
    var rate = parseFloat(10).toFixed(2); 
    var quantity = parseFloat(15).toFixed(2); 
    var sub_total = parseFloat((rate * quantity) + sub_total).toFixed(2);
  }
console.log(sub_total);

Expected Output:

450.00

Please help me How can I solve this why it giving output 150150150.00.

4
  • what is the expected value ? Commented Mar 28, 2018 at 12:42
  • It give me exact out as you wanted then what is an issue? Commented Mar 28, 2018 at 12:43
  • 3
    .toFixed() returns string Commented Mar 28, 2018 at 12:43
  • You will get 600.00 not 450.00. Because loop has 4 iterations. Commented Mar 28, 2018 at 12:47

6 Answers 6

2

Because subTotal is a string (since you used toFixed), make it

var sub_total = parseFloat((rate * quantity) + sub_total);
sub_total = sub_total.toFixed(2);

Demo

var sub_total = 0;
for (var i = 0; i < 3; i++) {
  var rate = parseFloat(10).toFixed(2);
  var quantity = parseFloat(15).toFixed(2);
  var sub_total = parseFloat((rate * quantity) + sub_total);
}
sub_total = sub_total.toFixed(2);
console.log(sub_total);

Edit

Expected Output:

450.00

Change the for-loop back to

for (var i = 0; i <= 3; i++) {
Sign up to request clarification or add additional context in comments.

1 Comment

why parseFloat for numbers?
0

Could this be the answer to your question?

As the others already said: toFixed returns a String

var sub_total = 0;
  for(var i = 0; i <= 3; i++){
  var rate = parseFloat(10); 
  var quantity = parseFloat(15); 
  var sub_total = parseFloat((rate * quantity) + sub_total);
}
console.log(sub_total);

2 Comments

Better, you need to explain what you have changed to achieve this answer.
@Durga Thank you for your hint. Others already told the solution but nevertheless I added this to mine.
0

Hope below code is helpful.

 var sub_total = 0;
 for(var i = 0; i <= 3; i++){
  var rate = parseFloat(10).toFixed(2); 
  var quantity = parseFloat(15).toFixed(2); 
  sub_total+ =parseFloat(rate) * parseFloat(quantity) + 
  parseFloat(sub_total);

  }
 console.log(sub_total);

Comments

0

You could use numbers without parseFloat. At the end, you could format the number.

You need only to iterate three times and assign the unchanging values only once.

var sub_total = 0,
    rate = 10,
    quantity = 15,
    i;

for (i = 0; i < 3; i++){
    sub_total += rate * quantity;
}

console.log(sub_total.toFixed(2));

Comments

0

For Expected Output: 450.00

You have to loop only <3 then you will be get required output.

See blow Demo

var sub_total = 0,
  rate = 10,
  quantity = 15,
  i = 0;

for (; i < 3; i++) {
  sub_total += (rate * quantity);
}


console.log(sub_total.toFixed(2));

2 Comments

dv, not me, but to much parseFloat for numbers in.
@NinaScholz thank for this "but to much parseFloat for numbers". Please see I have updated my answer.
-1

I have removed "<=" and replaced it with "<" since previous case leads to another iteration,because of that you get 600.00 .Also I have used toFixed method at the end, since initially we need to calculate the value of sub_total first then we have to convert it to a string having two decimals.

 var sub_total = 0;

  for(var i = 0; i < 3; i++){
    var rate = parseFloat(10).toFixed(2); 
    var quantity = parseFloat(15).toFixed(2); 
    sub_total = parseFloat((rate * quantity) + sub_total);
  }

    console.log(sub_total.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.