Home >Web Front-end >JS Tutorial >How to get an integer in javascript division
In javascript, you can use parseInt to take an integer in division. The syntax format is "parseInt (string, the base of the number to be parsed)". The "parseInt()" function parses a string and returns an integer.
The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.
Math.round(x) rounding, such as Math.round(0.70), the result is 1; Math.round(0.39), the result is 0;
Math.floor(x) to Rounding down, such as Math.floor(0.70) and Math.floor(0.39), the results are both 0;
Math.ceil(x) rounding up, such as Math.ceil(0.70) and Math. ceil(0. 39), the result is 1;
parseInt(x) discards the decimal part and retains the integer part, parseInt(3.14), the result is 3.
Math.floor():
_.each(cart_items,function(key){ var promotions_barcode=promotions_items[0].barcodes; if(promotions_barcode.indexOf(key.barcode)!=-1){ key.promotions_count=Math.floor(key.count/3);//获取3的整数倍,向下取整 } else{ key.promotions_count=0; } });
parseInt():
function getResult(num){ return parseInt(num); }
Math.ceil():
function getResult(num,n){ return Math.ceil(num); }
[Recommended learning: JavaScript advanced tutorial】
The above is the detailed content of How to get an integer in javascript division. For more information, please follow other related articles on the PHP Chinese website!