How to make the '0' digit

How to make the '0' digit
0

print to the console when the price is printed.

Does this help?

const gasPrice = new Intl.NumberFormat('en-US',
                        { style: 'currency', currency: 'USD',
                          minimumFractionDigits: 3 });
 
console.log(gasPrice.format(5.259)); // $5.259

Just to add to that: It is helpful if you provide the relevant code. I only know exactly what you’re doing here because I answered your last question.

You’re adding the prices up, what you have is correct from that perspective, because that’s how maths works: 4.00 is 4, 11.20 is 11.2, and 3.00 is 3. 4 + 11.2 + 3 is 18.2.

You treat the costs as a decimal numbers for calculation, but you have to format the result carefully to display it the way you want, because at that point it’s not just a decimal number you’re displaying: from a maths perspective. (And using the Itl method is how you fix it without having to manually split the cents off and apply padRight on the string).

// Created by Paul A. Gureghian in May 2020. //
// This JavaScript program creates a three-course meal //
// Based upon what is available on a menu. //

// Start of program. //

// Create an object named "menu". //
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 `Your meal is: ${appetizer.name}, ${main.name}, and ${dessert.name}, and the total price is $${totalPrice}`;
                                                                  
    }
};

// Add dishes to courses. //
menu.addDishToCourse('appetizers', 'salad', 4.00);
menu.addDishToCourse('appetizers', 'wings', 4.50);
menu.addDishToCourse('appetizers', 'fries', 5.00);

menu.addDishToCourse('mains', 'steak', 10.25);
menu.addDishToCourse('mains', 'salmon', 7.75);
menu.addDishToCourse('mains', 'pasta', 11.20);

menu.addDishToCourse('desserts', 'ice cream', 3.00);
menu.addDishToCourse('desserts', 'cake', 4.50);
menu.addDishToCourse('desserts', 'pie', 3.50);

// Call 'menu' object method 'generateRandomMeal()'. //
const meal = menu.generateRandomMeal();

console.log("\n");
console.log("Run the program and generate a random three-course meal:", "\n");
console.log(meal);

// End of program. //

So, I need to convert ‘totalPrice’ to a ‘Intl’ or ‘Itl’ variable ?

I also saw some pages after a Google search which mentioned the ‘toFixed’ method.

No, Itl is a built-in JS object with some methods, designed for internationalisation. Give the function described by @JeremyLT a number and tell it that you want it formatted as dollars, and it will return a correctly-formatted currency string

toFixed will also work, 18.2.toFixed(2) will return the string “18.20”

like this: const = totalPrice.toFixed(2) ?

Well, test it: your browser has a console where you can type code in to try it out

I got ‘toFixed()’ to work. thanks.

1 Like