This question already has answers here:
jQuery - Round to 2 decimal places and calculate with that number
(4 answers)
Closed 4 years ago.
$(document).ready(function() {
$('select').on('change', function() {
$('.total_result').text(
$('select[name=attendance]').val() * 0.25 + $('select[name=tardiness]').val() * 0.10 + $('select[name=rules]').val() * 0.05 + $('select[name=case]').val() * 0.05 + $('select[name=productivity]').val() * 0.15 + $('select[name=qa]').val() * 0.15 + $('select[name=utilization]').val() * 0.05 + $('select[name=schedule]').val() * 0.05 + $('select[name=fcr]').val() * 0.10 + $('select[name=aht]').val() * 0.05)
;
});
});
I have an issue with my final code. I need to round the decimal.
I'm using onchange that will auto calculate.
Much appreciated. TIA
.toFixed(2); method is Used to round value.
in you example it will be like
$(document).ready(function() {
$('select').on('change', function() {
$('.total_result').text(($('select[name=attendance]').val() * 0.25 + $('select[name=tardiness]').val() * 0.10 + $('select[name=rules]').val() * 0.05 + $('select[name=case]').val() * 0.05 + $('select[name=productivity]').val() * 0.15 + $('select[name=qa]').val() * 0.15 + $('select[name=utilization]').val() * 0.05 + $('select[name=schedule]').val() * 0.05 + $('select[name=fcr]').val() * 0.10 + $('select[name=aht]').val() * 0.05).toFixed(2));
});
});
Reference LINK
Related
I am trying to get the price_with_tax base on the given price without tax and tax rate on javascript and it works using the formula i mentioned.now I want to get the price_without_tax given the price_with_tax and tax_rate. I did it with
price_wo_tax = price_w_tax - (price_w_tax * (price_tax_rate / 100));
but our formula seems to be wrong? anyone who knows the reverse formula?
If you reverse your formula, you can get following:
price_wo_tax = (100 * price_w_tax) / (100 + price_tax_rate);
Here's explanation:
price_w_tax = price_wo_tax + (price_wo_tax * (price_tax_rate / 100))
transforms to
price_w_tax = price_wo_tax * (1 + price_tax_rate / 100))
transforms to
price_w_tax / (1 + price_tax_rate / 100)) = price_wo_tax
transforms to
price_wo_tax = 100 * price_w_tax / (100 + price_tax_rate)
If your taxrate is say x%, and your total amount is $y.
GST = y - y/(1+x/100)
Of course, if your taxrate is not in percent but is a fraction between 0 and 1, the formula gets
GST = y - y/(1+x)
I'm trying to generate random integers that are are multiples of 30 in JavaScript.
That is:
0 60 0 180 120 ...... and so on
in range between 0 to 360 for example
So I am looking for a function something like this:
function (_range,_multi)
{
Math.round(...);
return rndNum;
}
Generate a random number between 0 and 12 (range) and multiply by 30 (multi):
Math.floor(Math.random() * 12) * 30
This gives you [0, 360) (so you never get 360)
Here's a live demo that shows a full working function - the general idea is that you multiply by (max/multiple), floor the value, then multiply it by the multiple:
function generate(min, max, multiple) {
var res = Math.floor(Math.random() * ((max - min) / multiple)) * multiple + min;
return res;
}
alert(generate(0, 360, 30));
Seems like that should work.
function randomMultiple(max, mult) {
return Math.floor(Math.random() * (max / mult)) * mult;
}
Thus a call of randomMultiple(360, 30) would produce an element of G with
G = { y = 30 * x | 0 < x < 12 }
I have function which is generating random RGBA color and if I log it to the console it is rounding to on decimal place(that is what I want), but if you open console and try this as inline style attribute on body element you can see that it's not rounding to one decimal place ? Where is the problem ?
JSFIDDLE(backgroundColor)
setInterval(function foo (){
document.body.style.backgroundColor = ("rgba(" + Math.round(Math.random() * 255) + "," + Math.round(Math.random() * 255) + "," + Math.round(Math.random() * 255) + "," + Math.random().toFixed(1)) + ")";
},1000);
JSFIDDLE(console.log)
JS
setInterval(function foo (){
console.log(("rgba(" + Math.round(Math.random() * 255) + "," + Math.round(Math.random() * 255) + "," + Math.round(Math.random() * 255) + "," + Math.random().toFixed(1)) + ")");
},1000);
If you compare these two fiddles you can see the difference.So why are they different? It is same function and it should be same, right ?
The browser internally stores the alpha value as 1 byte, not as a float. When for example you say you need a 0.5 alpha, the browser converts that into 1 byte integer using this:
Math.floor(255 * 0.5)
When you query the result back, it wil convert that integer back to 0-1 range by dividing with 255.
This will result in an incorrect alpha. I've tested this using this:
rgba(134,78,73,0.5) //the color outputed by javascript
rgba(134, 78, 73, 0.498039) //the color that was read back from the style
0.498039 = Math.floor(255 * 0.5) / 255; (of course with some rounding)
so I have this code that is giving me a random number for both top and left attribute of some images.
var random1 = Math.ceil(Math.random() * 500);
var random2 = Math.ceil(Math.random() * 500);
$(document).ready(function () {
$('#randomp').css('top', random1);
$('#randomp').css('left', random2);
});
The problem is that I would be prefer to randomize a number between 1 and 100%. Is that possible?
Math.floor(Math.random() * 100) + 1 + '%'
Since Math.random returns number that is random, not less than 0 and less than 1, you have just to multiply result by 99 instead of 500 to get a number beetween 1 and 100%.
Finally, the code should be as follows:
var random1 = Math.round(Math.random() * 99) + 1;
var random2 = Math.round(Math.random() * 99) + 1;
This gives you a number between 0 and 100 (both included):
Math.floor(Math.random() * 101);
Math random will give you a number between 0 (included) and 1 (excluded)
If you multiply that number with 101, it will give you a number between 0 (included) and 101 (excluded)
Math.floor will return the the largest integer less than or equal to the above number.
Not sure if you want it rounded or not, so here's both:
console.log( rando(1, 100) + "%" );
console.log( rando(1, 100, "float") + "%" );
<script src="https://randojs.com/1.0.0.js"></script>
This uses randojs.com to make the randomness simple and readable. If you need to know more, check out the website.
I need to round up to the nearest 0.10 with a minimum of 2.80
var panel;
if (routeNodes.length > 0 && (panel = document.getElementById('distance')))
{
panel.innerHTML = (dist/1609.344).toFixed(2) + " miles = £" + (((dist/1609.344 - 1) * 1.20) + 2.80).toFixed(2);
}
any help would be appreciated
var number = 123.123;
Math.max( Math.round(number * 10) / 10, 2.8 ).toFixed(2);
If you need to round up, use Math.ceil:
Math.max( Math.ceil(number2 * 10) / 10, 2.8 )
Multiply by 10, then do your rounding, then divide by 10 again
(Math.round(12.362 * 10) / 10).toFixed(2)
Another option is:
Number(12.362.toFixed(1)).toFixed(2)
In your code:
var panel;
if (routeNodes.length > 0 && (panel = document.getElementById('distance')))
{
panel.innerHTML = Number((dist/1609.344).toFixed(1)).toFixed(2)
+ " miles = £"
+ Number((((dist/1609.344 - 1) * 1.20) + 2.80).toFixed(1)).toFixed(2);
}
To declare a minimum, use the Math.max function:
var a = 10.1, b = 2.2, c = 3.5;
alert(Math.max(a, 2.8)); // alerts 10.1 (a);
alert(Math.max(b, 2.8)); // alerts 2.8 because it is larger than b (2.2);
alert(Math.max(c, 2.8)); // alerts 3.5 (c);
This is a top hit on google for rounding in js. This answer pertains more to that general question, than this specific one. As a generalized rounding function you can inline:
const round = (num, grainularity) => Math.round(num / grainularity) * grainularity;
Test it out below:
const round = (num, grainularity) => Math.round(num / grainularity) * grainularity;
const test = (num, grain) => {
console.log(`Rounding to the nearest ${grain} for ${num} -> ${round(num, grain)}`);
}
test(1.5, 1);
test(1.5, 0.1);
test(1.5, 0.5);
test(1.7, 0.5);
test(1.9, 0.5);
test(-1.9, 0.5);
test(-1.2345, 0.214);
var miles = dist/1609.344
miles = Math.round(miles*10)/10;
miles = miles < 2.80 ? 2.80 : miles;
to round to nearest 0.10 you can multiply by 10, then round (using Math.round), then divide by 10
Round to the nearest tenth:
Math.max(x, 2.8).toFixed(1) + '0'
Round up:
Math.max(Math.ceil(x * 10) / 10, 2.8).toFixed(2)