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?
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.
$(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));
});
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.
try OnSaleAT.toFixed(1)+'0' :)
Math.ceil(1000* (1 - OnSale/RegPrice))/10
give you "25.5"
:)
Related
Having some trouble getting this right. I'm very new to jQuery, so trying to get better and learn.
Currently I am getting 2 different values from a html table using the following code
var sellPrice = $('.qt').find("tr:eq(2)").find("td:eq(4)").html();
var buyPrice = $('.break .main-col .qt').find("tr:eq(2)").find("td:eq(4)").html();
These both output a value such as $13,000,000
I am then wanting to subtract 1 from these values (making it $12,999,999) before pasting them to an input as such
$('input[name="sell"]').val(sellPrice);
$('input[name="buy"]').val(buyPrice);
However, I am having some trouble with how to subtract $1 from these.
I tried using sellPrice--; but without success.
I've also tried adding - 1; at the end of each variable, but did not succeed either.
I tried to test something like this, but did not work either.
var minusOne = -1;
var getCurrentSellPrice = $('.qt').find("tr:eq(2)").find("td:eq(4)").html();
var getCurrentBuyPrice = $('.break .main-col .qt').find("tr:eq(2)").find("td:eq(4)").html();
var sellPrice = (getCurrentSellPrice - minusOne);
var buyPrice = (getCurrentBuyPrice - minusOne);
$('input[name="sell"]').val(sellPrice);
$('input[name="buy"]').val(buyPrice);`
Trying my best to familiarize myself with jQuery :)
Any help is much appreciated!
Solved using this
var getCurrentSellPrice = $('.qt').find("tr:eq(2)").find("td:eq(4)").html();
var getCurrentBuyPrice = $('.break .main-col .qt').find("tr:eq(2)").find("td:eq(4)").html();
var sellPrice = Number(getCurrentSellPrice.replace(/[^0-9\.]+/g,"")) - 1;
var buyPrice = Number(getCurrentBuyPrice.replace(/[^0-9\.]+/g,"")) + 1;
$('input[name="sell"]').val(sellPrice);
$('input[name="buy"]').val(buyPrice);
Since your numbers contain currency symbol and are strings, you need to convert them to proper numbers before subtracting them. See the answer below.
How to convert a currency string to a double with jQuery or Javascript?
var oneX = eval(prompt ("What's the x variable of the first set of points"));
var oneY = eval(prompt ("What's the y variable of the first set of points"));
var twoX = eval(prompt ("What's the x variable of the second set of points"));
var twoY = eval(prompt ("What's the y variable of the second set of points"));
console.log(oneX);
console.log(oneY);
console.log(twoX);
console.log(twoY);
var yRes = twoY-oneY;
var xRes = twoX-oneX;
console.log(yRes);
console.log(xRes);
var slope = xRes/yRes;
console.log("The slope is " + yRes + "/" + xRes);
This is my code. This is what the output looks like:
0.9166666666666666
0.8
2.09375
0.5714285714285714
-0.22857142857142865
1.1770833333333335
The slope is -0.22857142857142865/1.1770833333333335
However, I don't want -.22/1.177 I want -33/7 (I don't know the exact answer) I've never posted on StackOverflow before, so if I did something stupid while typing, I apologize. I'm new to programming so don't be too harsh.
The easiest way would be rounding to a certain fraction, e.g:
Math.round( slope * 10 ) + "/10"
What you're talking about is simplifying a ratio. First you multiply each number by the least common multiple to get them both into whole numbers, then divide each number by the greatest common factor to reduce them.
A full explanation of the process with examples: https://www.tutorialspoint.com/ratios_and_unit_rates/simplifying_ratio_decimals.htm
I am trying to calculate an angle in js using math js. I am experienced that when the division is between negative numbers js give me bad results. e.g. -6/-3 give me 20093 instead of 2.
How can I solve this? here below you can see a portion of console.log.
Here is the code:
var num = math.eval(parseInt(p[1]) - parseInt(d3.event.y));
var den = math.eval(parseInt(p[0]) - parseInt(d3.event.x));
if (den==0){
var angle = 0;
}else{
var m = math.eval(num/den);
if(m<1){
theta = m*100;
}else{
theta = m*100;
}
}
Syntax in code is num/den as you can see.
Thanks in advance
You can simply do -6/-3.
Run this in your developer tools console: alert(-6/-3); and you will see.
I think math.eval() expects a string like "-6/-3".
My code:
var $label = $(ev.currentTarget);
var $price = $label.parent("form").find(".oe_price .oe_currency_value");
if (!$price.data("price")) {
$price.data("price", parseFloat($price.text()); //price is 10.30 but it returns 10.3 as number
}
$price.html($price.data("price")+parseFloat($label.find(".badge span").text() || 0));
/* The value coming in **badge** is **12.00**
* but parseFloat converts that into **12**
* thats why after the addition I got the output as **22.3**
* but i want it as **22.30**
*/
I have a string
'10.30'
Now, if I convert the string to number using parsefloat
parseFloat('10.30')
I got the output as 10.3
And If I do it using the .tofixed(2)
parseFloat('10.30').toFixed(2)
I got the output 10.30 but it is in STRING which is the big problem for me because I want the output as number.
And if i do like this
Number(parseFloat('10.30').toFixed(2))
I got the output 10.3
But i want the output in number and with decimal point
like this 10.30
Plz help...!!!
thankz Passerby for the help.
Finally i got the result from this :
$price.html(($price.data("price")+parseFloat($label.find(".badge span").text() || 0,10)).toFixed(2));
Try this:
parseFloat(Math.round(num3 * 100) / 100).toFixed(2);
See Live Demo
I don't know what are you doing, but I've done the same and got desired result.
My code:
var str = '10.30';
var flo = parseFloat(str);
alert(flo);
flo = flo.toFixed(2)
alert(flo);
See here: Fiddle
Trying to create a loan calculator based on two sliders. I feel I'm nearly there but the syntax is letting me down plus one last formula.
I have two sliders, one which represents the loan amount, the other represents the loan length.
Here is a bit of the code to highlight all the calculations. I think there are errors here too.
function update() {
$amount = $("#slider1").slider("values", 100);
$interest = $amount / 100 * 15 ;
$perday = 15 ;
$apr = (($interest / $amount) / ("#slider2"/365) * 10000) / 100;
$amount2 = $amount + $interest;
$("#amount").val($amount1);
$("#amount2").val($amount2);
$("#amount3").val($interest);
}
Interest is charged at 15% of the amount borrowed.
Each day is worth 15p so in order to get my final charge.
[15% of the loan amount total]
– [0.15p per day credit]
I have developed a fiddle but its not correct, hence why I'm here.
Fiddle Here
How can I get both the sliders to work together so that if I move the top or bottom slider, it will affect the overall loan amount and Interest?
Any help will be most appreciated. I'm really struggling with this one.
I believe this is what you want - jsfiddle <- follow link
function update() {
$interest = 0.15 ;
$perday = 15 ;
$amount1 = $("#amount").val();
$dayscount = $("#days").val();
$amount2 = parseInt($amount1) + $interest * parseInt($amount1) + (parseInt($dayscount) * ($perday/100));
$("#amount2").val($amount2);
$("#amount3").val(parseFloat($amount2-$amount1).toFixed(2));
}
Fixed your update algorithm and your slider handlers
I considered that you last 2 fields (Your Loan & Interest) are the final value to be paid and the difference between the value to be paid and the value borrowed respectively. If this interpretation is not what you intended please comment.
UPDATE 1
Here I updated the jsfiddle. Beware that I don't know what the APR is, so validate that my calculation are right. I also did not use any rounding, cause I don't know if you need it like this
UPDATE 2
Updated with new formula here. I still have no idea if this is right or not
If i understood you right, you could do something like this:
function update() {
var interest = 0.15;
var sliderAmount = parseFloat($("#slider1").slider("option", "value"));
var days = parseFloat($("#slider2").slider("option", "value"));
var perday = 15 * days ;
var interestAmount = sliderAmount * interest * days;
$("#amount1").val(sliderAmount);
$("#amount2").val(sliderAmount);
$("#amount3").val(interestAmount+ perday);
}