当前位置:网站首页>JS how to judge when data contains integer and floating-point types. Floating-point decimals retain two digits after the decimal point

JS how to judge when data contains integer and floating-point types. Floating-point decimals retain two digits after the decimal point

2022-06-26 12:23:00 Yotoai Mengtian

Throw questions :

        When writing code today , Find out ajax Traversal data The number of digits after the floating point decimal point is too long ( There are many after the decimal point 0), Use .toFixed(2) when , Although the two decimal places are left after the floating point , however Integer data More .00 ( One more point and two zeros ).

resolvent :

        Just go through the data , Add judgement :

var html = '';
for(var i = 0; i < data.length;i++){
    html +='<tr><td>' + data[i].data? (parseInt(data[i].data)!=data[i].data?data[i].data.toFixed(2):data[i].data):0 + '</td></tr>'
}

 

原网站

版权声明
本文为[Yotoai Mengtian]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202170520382315.html