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);
Related
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.
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);
This question already has answers here:
Is there a JavaScript function that can pad a string to get to a determined length?
(43 answers)
Closed 6 years ago.
Sorry for the misunderstanding title, I don't know how could explain better.
That's my problem: I have this loop
for(i=1; i<1000; i++){
"MyName_"+i;
}
This will give the following
MyName_1
MyName_2
...
MyName_10
...
MyName_100
How can I do, in a easy way, to have the same number of digits anytime?
This means
MyName_001
...
MyName_010
...
MyName_100
obviously without doing stuff like
if(i<10)
...
if((i>10)&&(i<100))
because the input number is a input so it may be 1000, 10000 or 10000000 and I don't want to write tons of "if()"...
Thank you
Use JavaScript String#slice method
var input = 100; //input any number
var len = input.toString().length;
for (i = 1; i < input; i++) {
console.log("MyName_" + ('000000000000000' + i).slice(-len));
}
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);
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