skip to Main Content

i am doing an ecommerce project.

Regular price is $549

12.96% is OFF

if i minus 12.96% from $549

Sale price becomes $477.8496

As it can be seen in below screenshot.

enter image description here

I want to show sale price close to a round figure either it should be $477 or $478

here is the code.

{item.price - item.price *  item.discountPercentage/100 }

4

Answers


  1. let price = 477.8496;

    let formattedPrice = Math.round(price);

    Adjust the variable names and values based on your specific use case. The key idea is to use a rounding function (like round() in Python or Math.round() in JavaScript) to round the price to the nearest whole number and then format it as needed.

    Login or Signup to reply.
  2. Math.floor() is a JavaScript method that returns the largest integer less than or equal to a given number.

    Math.floor(477.8496); // returns 477

    Math.ceil() function returns the smallest integer greater than or equal to a given number.

    Math.ceil(477.8496); // returns 478

    Login or Signup to reply.
  3. To round the number 477.888 either up to 488 or down to 477 in JavaScript, you can use the Math.ceil() method to round up or the Math.floor() method to round down. Here’s how you can achieve both:

    1. Round up to 488:
    var roundedUp = Math.ceil(477.888);
    console.log(roundedUp); // Output: 478
    
    1. Round down to 477:
    var roundedDown = Math.floor(477.888);
    console.log(roundedDown); // Output: 477
    
    Login or Signup to reply.
  4. You can round off the decimal values with price.toFixed(). You can also pass a integer argument to round up to the number of decimal places. (ex: price.toFixed(2)).

    parseInt(price) is another way to eliminate the decimal values.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search