Meal Maker returning strange decimals

Logging this to console, I get strange numbers sometimes.
Example:
“You ordered the caesar salad, trout, and ice cream. Your bill comes to $25.569999999999997.”
How can I keep it limited to two decimal places?

const menu = {
  _courses: {
    appetizers: [],
    mains: [],
    desserts: [],
  },
  get appetizers() {
    return this._courses.appetizers;
  },
  get mains(){
    return this._courses.mains;
  },
  get desserts(){
    return this._courses.desserts;
  },
  set appetizers(appetizers) {
    this._courses.appetizers = appetizers;
  },
  set mains(mains) {
    this._courses.mains = mains;
  },
    set desserts(desserts) {
    this._courses.desserts = desserts;
  },
  get courses() {
      return {
        appetizers: this.appetizers,
        mains: this.mains,
        desserts: this.desserts
      };
    },
    addDishToCourse(courseName, dishName, dishPrice) {
  const dish = {
    name: dishName,
    price: dishPrice,
  };
      return this._courses[courseName].push(dish);
  },
  getRandomDishFromCourse(courseName) {
      const dishes = this._courses[courseName];
      const randomIndex = Math.floor(Math.random() * dishes.length);
      return dishes[randomIndex];
  },
  generateRandomMeal() {
  const appetizer = this.getRandomDishFromCourse('appetizers');
  const main = this.getRandomDishFromCourse('mains');
  const dessert = this.getRandomDishFromCourse('desserts');
  const totalPrice = appetizer.price + main.price + dessert.price;
  return `You ordered the ${appetizer.name}, ${main.name}, and ${dessert.name}. Your bill comes to $${totalPrice}.`;
  }
};
  
menu.addDishToCourse('appetizers', 'caesar salad', 2.99);
menu.addDishToCourse('appetizers', 'green salad', 3.49);
menu.addDishToCourse('appetizers', 'choregi salad', 4.19);

menu.addDishToCourse('mains', 'steak', 26.99);
menu.addDishToCourse('mains', 'lobster', 25.09);
menu.addDishToCourse('mains', 'trout', 20.49);

menu.addDishToCourse('desserts', 'pudding', 3.99);
menu.addDishToCourse('desserts', 'ice cream', 2.09);
menu.addDishToCourse('desserts', 'tiramisu', 4.79);

const meal = menu.generateRandomMeal();
console.log(meal);

use the toFixed method:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toFixed

1 Like

This topic was automatically closed 60 minutes after the last reply. New replies are no longer allowed.