Javascript sum of values in table [duplicate] - javascript

This question already has answers here:
How to format a float in javascript?
(14 answers)
Formatting a number with exactly two decimals in JavaScript
(32 answers)
Closed 9 years ago.
I've succesfully used this code to calculate sums in a table:
var $overall = 0;
$("tr.sum").each(function()
{
var $qnt = $(this).find("td").eq(0);
var $price = $(this).find("td").eq(1);
console.log($qnt+" | "+$price);
var sum = parseFloat($price.text()) * parseFloat($qnt.text());
$(this).find("td").eq(2).text(sum);
$overall+= sum;
});
$("#total").text($overall); });
I changed one line to avoid rounding errors:
var sum = parseFloat((parseFloat($price.text().replace(',', '.')) * parseFloat($qnt.text().replace(',', '.'))).toFixed(2));
It works fine. But I can't solve the problem to round the total sum to two decimal places.
$("#total").text($overall);
I tried toFixed(2). But this results in a string and not a number.
Any help would be appreciated!
Thanks, Mike

after doing all calculation make it like this
$overall.toFixed(2);

Related

Use of bitwise OR operation in TypedArray.prototype.subarray() call in Javascript [duplicate]

This question already has answers here:
using bitwise OR in javascript to convert to integer
(1 answer)
How do I convert a float number to a whole number in JavaScript?
(18 answers)
Closed 2 years ago.
The following code is from https://developer.mozilla.org/en-US/docs/Web/API/TextEncoder/encodeInto under heading "Encode Into A Specific Position".
var encoder = new TextEncoder;
function encodeIntoAtPosition(string, u8array, position) {
return encoder.encodeInto(string, position ? u8array.subarray(position|0) : u8array);
}
var u8array = new Uint8Array(8);
encodeIntoAtPosition("hello", u8array, 2);
console.log( "" + u8array.join() ); // 0,0,104,101,108,108,111,0
In encodeIntoAPosition, what is the reason for position|0?
Here are some results for bitwise OR operations.
console.info([ 2|0, 2.5|0, 'z'|0, 'abc'|0, -2|0, -2.5|0 ]);
Thanks.

how to make lower decimal javascript? [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 3 years ago.
I am trying to lower decimal, can anyone help me to show how to use "toFixed" in this code? or if there is any other way?
miniseconds/1000/60/60/24 so I get the number of days.
function () {
try {
if ({{cookie - firstSeen}}) {
var now = new Date().getTime()
var time2conversion = now - {{cookie - firstSeen}}
return time2conversion/1000/60/60/24
}else{
return undefined
}
}catch(e) {
return undefined;
}
}
The result is 1.3964438888888886, but it would be for ex. 1.39.
u can follow this format
var number.toFixed(2);

Number decimal is displaying wrong [duplicate]

This question already has answers here:
javascript - how to prevent toFixed from rounding off decimal numbers
(7 answers)
Closed 5 years ago.
I am defining my numberfield and then I amgetting some value like this
val = 3.555678
I am using decimalPrecision : 3 in my number field so The value is displaying is 3.556
I want my value should be display 3.555 so for that I am using
val = val.toFixed(3);
val is coming in consol 3.555
then numberfield.setValue(val);
But in UI it is still comming 3.556
Why it is coming this.
I you want to display 3.555 you can do it in the following way
let val = 3.555678
let decimalPrecision = 3;
let roundedval = val.toFixed(3);
if(roundedval > val){
val = roundedval - Math.pow(10, -1*decimalPrecision);
}
console.log(val);
You can use:
var val=3.555678;
val = Math.floor (val*1000)/1000;
console.log(val);

Javascript Maths showing wrong number [duplicate]

This question already has answers here:
How to force JS to do math instead of putting two strings together [duplicate]
(11 answers)
Closed 7 years ago.
I am trying to do maths and i been getting values such as 200052 i.e if i add 2000+52, where it should be 2052. What am i doing wrong or missing something?
//get value of amount eneterd
amount = document.getElementById("amount").value;
//apply percentage
if(rateExcel.checked = true){
total= parseINT((amount/100) * 5);
total= total + parseINT(amount);
//still i get the same, like 20052 instead of 252.
};
Edit: The input is integer, not a string! I see many people trying to be ninja about telling to use parseINT(), but i tried and it didn't work.
You fix this by forcing the amount variable to become an int since it's probably received as a string.:
total = total + parseInt(amount);
Your amount is a string so you need to convert your amount to number using
total= total + Number(amount);
//get value of amount eneterd
amount = document.getElementById("amount").value;
//apply percentage
if(rateExcel.checked == true){
total= (parseInt(amount,10)/100) * 5;
total= total + parseInt(amount,10);
};

How to generate random numbers in Javascript [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Generating random numbers in Javascript
Hi..
I want to generate random numbers (integers) in javascript within a specified range. ie. 101-999. How can I do that. Does Math.random() function supports range parameters ?
Just scale the result:
function randomInRange(from, to) {
var r = Math.random();
return Math.floor(r * (to - from) + from);
}
The function below takes a min and max value (your range).
function randomXToY(minVal,maxVal)
{
var randVal = minVal+(Math.random()*(maxVal-minVal));
return Math.round(randVal);
}
Use:
var random = randomXToY(101, 999);
Hope this helps.
Math.floor(Math.random()*898)+101

Categories

Resources