Reputation: 325
junior programmer here. I am currently working on rounding number function
so, at first, there would be a price for each item. and then there is an extra percentage button to increase the item price based on the value inserted.
So let's say item A's price is $1, then I insert value 25% into the percentage. it will give me 1.33. How to write a function that will return 1.35 instead of 1.33?
Another example, $2.67 becomes $2.70, $12.34 becomes $12.35
here is my code
var price = parseFloat(items.price); //1
var percentage = parseFloat(vm.data.percentage); //25%
added_price = parseFloat(price) / (parseFloat(100) - parseFloat(percentage)) * 100; //formula
items.price = parseFloat(added_price).toFixed(vm.data.decimal); //the result = 1.33
Can somebody help me please?? thank you :)
Upvotes: 1
Views: 123
Reputation: 167250
Steps to do this "custom" rounding off.
:)
Multiply by 2
and round it off. And FYI, it would be 2.65
and not 2.70
. You need to use Math.ceil()
and not round
.
var p1 = 2.67;
var p2 = 12.34;
function round_to_custom(num) {
num *= 20;
num = Math.round(num);
num /= 20;
return num;
}
console.log(round_to_custom(p1));
console.log(round_to_custom(p2));
Using ceil
(for getting more $$$ from customers):
var p1 = 2.67;
var p2 = 12.34;
function round_to_custom(num) {
num *= 20;
num = Math.ceil(num);
num /= 20;
return num;
}
console.log(round_to_custom(p1));
console.log(round_to_custom(p2));
Upvotes: 1