0
votes

I'm trying to get the percentage between two numbers for the purpose of showing the difference as far as a discount. I've tried to simplify it as much as I can but I still cant get what I want.

Here is an example.

var RegPrice = 8.95;
var OnSale = 6.67;
var OnSaleAT =  Math.abs(Math.max(100.00 - OnSale / RegPrice * 100.00));
alert(OnSaleAT.toFixed(2));

What I'm trying to get is the alert(); to return a value of 25.50. However, I'm getting 25.47.

Any ideas on how I can get this right?

5
You don't need jQuery for basic arithmetic operations. Also, the discount of 25.47% is correct. - Rob W
what it has to do with jQuery, jQuery is not javascript - Anurag Uniyal
6.67 / 8.95 = 0.74525, 100 - 74.525 = 25.475, fix at 2 decimal points is 25.47. If you want to round, you'll need to use Math.round(). - zzzzBov

5 Answers

4
votes

25.47486... is the correct answer. If you're attempting to round to the nearest tenths, you can use:

var result = Math.round(OnSaleAT * 10) / 10;

Which outputs: 25.5 and from there you can format your answer how you like.

0
votes
$(window).load(function() {

    var RegPrice = 8.95;
    var OnSale = 6.67;
    var OnSaleAT = Math.round(Math.abs(Math.max(100.00 - 6.67 / 8.95 * 100.00)) * 10.00) / 10.00

    alert(OnSaleAT.toFixed(2));
});
0
votes

If you're trying to round to 25.5%, you can just alert with a toFixed param of 1 instead of 2:

alert(OnSaleAT.toFixed(1));

Other than that, Rob W is right...the math does come out to 25.47, and there's not much you can do about that.

0
votes

try OnSaleAT.toFixed(1)+'0' :)

0
votes

Math.ceil(1000* (1 - OnSale/RegPrice))/10

give you "25.5"

:)