• 技术文章 >Web开发 >JavaScript

    js将小数转为整数的方法

    小妮浅浅小妮浅浅2021-09-11 16:16:28原创7196

    1、使用“parseInt(小数值)”语句。

    document.write(parseInt("10") + "
    ");
    document.write(parseInt("10.33") + "
    ");
    document.write(parseInt("34 45 66") + "
    ");
    document.write(parseInt(" 60 ") + "
    ");
    document.write(parseInt("40 years") + "
    ");
    document.write(parseInt("He was 40") + "
    ");
    document.write("
    ");
    document.write(parseInt("10",10)+ "
    ");
    document.write(parseInt("010")+ "
    ");
    document.write(parseInt("10",8)+ "
    ");
    document.write(parseInt("0x10")+ "
    ");
    document.write(parseInt("10",16)+ "
    ");

    2、使用“~~小数值”语句。

    var decimal=4;
    var integer = ~~decimal; // 4 = ~~4.123
    console.log(integer);

    3、使用“Math.floor(小数值)”语句。

    console.log(Math.floor(2.5));  //2
    console.log(Math.floor(-2.5));  //-3

    4、使用“Math.ceil(小数值)”语句。

    console.log(Math.ceil(2.5));  //3
    console.log(Math.ceil(-2.5));  //-2

    5、使用“Math.round(小数值)”语句。

    console.log(Math.round(2.5));  //3
    console.log(Math.round(-2.5));  //-2
    console.log(Math.round(-2.6));  //-3

    以上就是js将小数转为整数的方法,希望对大家有所帮助。更多Javascript学习指路:Javascript

    专题推荐:js 小数 整数
    品易云
    上一篇:javascript中eval的用法 下一篇:javascript实现下载的方法

    相关文章推荐

    • 面试准备:快速学习JS防抖与节流• javascript中JSON.stringify的注意点• js字符串中的三种引号• js盗用构造函数的实现

    全部评论我要评论

    © 2021 Python学习网 苏ICP备2021003149号-1

  • 取消发布评论
  • 

    Python学习网