[英]Javascript always returns float numbers
Thats my jquery code. 多数民众赞成在我的jQuery代码。 And the variable "procenti" always return like 92.3076923076923 % long decimal number.
并且变量“ procenti”总是返回长92.3076923076923%的十进制数。 I would like that number to be without the decimals, only 92%.
我希望该数字不包含小数位,只有92%。 I tried .toFixed() method but it doesnt works.
我尝试了.toFixed()方法,但是它不起作用。
When the equation is like procenti=(3/10)*100; 当等式为procenti =(3/10)* 100时; == 30%, the number in html looks just what i want without decimals.
== 30%,html中的数字只是我想要的,没有小数。
for(var i=1; i<14; i++){
var zmage=parseFloat($('#zm'+i).text());
var odigrane=parseFloat($('#od'+i).text());
var procenti=0;
if(zmage == 0){
$('#pr'+(i)).html(0 + ' %');
}
else{
procenti=(zmage/odigrane)*100;
$('#pr'+(i)).text(procenti + ' %');
}
}
round() function will round off value. round()函数将舍入值。
Math.round(procenti);
Otherwise there are another functions ceil() & floor() 否则,还有另一个函数ceil()和floor()
You should use either 您应该使用
Math.floor()
It returns the largest integer less than or equal to a given number. 它返回小于或等于给定数字的最大整数。
or 要么
Math.ceil()
It returns the smallest integer greater than or equal to a given number. 它返回大于或等于给定数字的最小整数。
or 要么
Math.round()
It returns the value of a number rounded to the nearest integer. 它返回四舍五入到最接近整数的数字的值。
It depends on what you actually want. 这取决于您的实际需求。
You could do something like this: 您可以执行以下操作:
function strip(number) {
return (parseFloat(number).toPrecision(12));
} }
Hope this helps && pozdrav iz Velenja! 希望这对&& pozdrav iz Velenja有所帮助! :)
:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.