Home  >  Article  >  Web Front-end  >  How to get an integer in javascript division

How to get an integer in javascript division

醉折花枝作酒筹
醉折花枝作酒筹Original
2021-06-15 16:25:106432browse

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.

How to get an integer in javascript division

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn