How to avoid scientific notation for large numbers in JavaScript?

后端 未结 22 2520
無奈伤痛
無奈伤痛 2020-11-22 00:31

JavaScript converts a large INT to scientific notation when the number becomes large. How can I prevent this from happening?

22条回答
  •  我在风中等你
    2020-11-22 00:48

    You can loop over the number and achieve the rounding

    // functionality to replace char at given index

    String.prototype.replaceAt=function(index, character) {
        return this.substr(0, index) + character + this.substr(index+character.length);
    }
    

    // looping over the number starts

    var str = "123456789123456799.55";
    var arr = str.split('.');
    str = arr[0];
    i = (str.length-1);
    if(arr[1].length && Math.round(arr[1]/100)){
      while(i>0){
        var intVal = parseInt(str.charAt(i));
    
       if(intVal == 9){
          str = str.replaceAt(i,'0');
          console.log(1,str)
       }else{
          str = str.replaceAt(i,(intVal+1).toString()); 
          console.log(2,i,(intVal+1).toString(),str)
          break;
       }
       i--;
     }
    }
    

提交回复
热议问题