JSNoob
JSNoob

Reputation: 5

Round up to 2 decimal places (Javascript)

I want to round up the result of this to 2 decimal places. I already tried Math.floor*(Math.pow (1.02,7)*100)/100 But I get 1,150 instead of 1,148.69 - the answer I aim to be returned.

A snippet of my code atm:

function money (amount) {

  return amount*(Math.pow (1.02,7))
}

money(1000);

Upvotes: 0

Views: 124

Answers (3)

StackSlave
StackSlave

Reputation: 10627

Here is how .toFixed() is used:

function money(amount){
  return (amount*Math.pow(1.02, 7)).toFixed(2);
}
console.log(money(1000));

Upvotes: 0

Darth Vader
Darth Vader

Reputation: 921

function money (amount) {
    return  Math.round(((amount*(Math.pow (1.02,7))) * 100)) / 100;
}
console.log(money(1000));

This will give the

Upvotes: 1

Lex
Lex

Reputation: 5014

You can use toFixed to round to a certain decimal

function money (amount) {

  return (amount*(Math.pow (1.02,7))).toFixed(2)
}

console.log(money(1000))

Upvotes: 3

Related Questions