foo.setAttribute("item-position", ""+bar+"");
The bar variable is a number, for example 1 or 15. What's the way to increase it on 1, so it would be 2 and 16 ?
May be something like this? But it doesn't work.
foo.setAttribute("item-position", ""+bar+""+1);
// The result should be 2 and 16, but here the result is 21 and 161.
// That is not what I want.
You're currently appending the number to the end of the string, this has nothing to do with arithmetic.
Just add the calculated result
foo.setAttribute("item-position", bar+1);
You don't have to turn it into a string, setAttribute will do that part.
Or if you want to increase the value in bar and show it, use the preincrement operator:
foo.setAttribute("item-position", ++bar);
Related
So, I have an object.
var position = {
x: 1,
y: 0
}
After few operations x or y can become something like 0.3700000000000001. However, I need it to be 0.37. I tried:
position[x].toFixed(2)
What is the right way to get get rid of 00000000000001 ?
position[x] will get property of the position object which will be equal to value variable x. You should use position.x.
And you also need to change the value of position.x because position.x.toFixed(2) will not change the value.
position.x = position.x.toFixed(2)
toFixed() returns a string. You should convert it to float again using +.
console.log(+(0.3700000000000001). toFixed(2))
There should not be any problem except using the string key or dot notation:
var position = {
x: 0.3700000000000001,
y: 0
}
position.x = +position.x.toFixed(2); // + converts the resulted string to number
console.log(position.x);
If you want a number, there is no way to drop 00000000000001 to get 0.37 exactly. Like for other languages (most hardware supports IEEE 754), the number 0.37 does not exists. If scanning 0.37 it is rounded to the nearest possible number: 0.37.toPrecision(18) shows "0.369999999999999996".
Because of this, toFixed() returns a string and not a number. So it is nonsense to use toFixed() and convert the result back to a float.
The easiest solution is to live with these unexact values and use toFixed() for output only (that is the idea of toFixed()).
Another solution for e.g. money is, to store all values as cent/penny and divide the values by 100 only on output: ($cent/100).toFixed(2)
You are getting the value but you are not storing it, so the original vulea is not changed. You need to do:
var position = {
x: 1,
y: 0
}
position.x=position.x.toFixed(2)
I have a variable like val=1
I have applied certain condition.
When the condition is true then val is multiplied by 3.
like 3, 9, 27, 81, 243..........
but after a long loop it shows like -715827883 value.
Why?
I want my result like,when i start to execute my loop reversely then val/3 is executed and at the end it will give 1.
But now it's giving 0.
Please suggest.
I am new to JavaScript, I want to get the right page count.
if one page the item count is 20, and the page count is 23, the page should be 2.
var count = 23
var per_page_count = 20
If in other language we can use:
count / per_page_count + 1
to get the page count, but in JavaScript we can not get it.
I also tried use Math.round, still not work
console.log(Math.round(count/per_page_count)) // there I want to get 2, but get 1
You can use
Math.ceil(count/per_page_count)
The Math.ceil() function returns the smallest integer greater than or equal to a given number.
from Math.ceil document.
I think you are trying to implement some sort of pagination. So I would suggest :
Maths.ceil(count/per_page_count)
I'm trying to format a percentage to have three significant figures. I'd like a fairly small percentage, something like 1075 / 107175175, to show up as 0.0001%.
var x = 1075 / 107175175;
console.log(x.toLocaleString('en-us', {
style: 'percent', minimumSignificantDigits: 1
}));
What I'm getting instead is '0.00100303078581397%.' Is this an issue with how floating point numbers are implemented in Javascript or a bug?
Have you tried using maximumSignificantDigits: 1?
console.log(x.toLocaleString('en-us', {
style: 'percent', maximumSignificantDigits: 1
}));
It's a small logical mistake, minimumSignificantDigits means show at least N numbers, while in your case i think you want to use maximumSignificantDigits which means show maximun 1 significant digit.
I'm having a problem with adding numbers in javascript. I have a variable that keeps track of full number (currentfloatx) and a variable that keeps track of the floored version of that number (newintx). I'm trying to add .25 to currentfloatx, but for some reason its multiplying the number by 10. Does anyone know why it's doing this? Is there something about how javascript is handling these number that I'm missing? Thanks.
//currentfloatx is currently set to 6
alert(currentfloatx + .25); //returns 60.25
alert(currentfloatx); //returns 6
newintx = Math.floor(currentfloatx + .25);
alert(newintx); //returns 60.25
The only way I can think of that would make this happen is if currfloatx holds a string (eg., "6"). This would make currfloatx + .25 be the equivalent of "6" + "0.25", or "60.25".