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.
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
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.
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
To round the number
477.888
either up to488
or down to477
in JavaScript, you can use theMath.ceil()
method to round up or theMath.floor()
method to round down. Here’s how you can achieve both:488
:477
: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.