Nodejs Int Parse toInt()

Here you can find the source of toInt()

Method Source Code

const string = '10.5';
String.prototype.toInt = () => parseInt(string);
String.prototype.toFloat = () => parseFloat(string);
const convert = string.toInt();//from   w  w  w.j  av  a 2s. com
const convertToFloat = string.toFloat();

console.log('------------------------------------------');
console.log('result chaining:');
console.log('------------------------------------------');
console.log('string.toInt()\n', convert);
console.log(`Type Data: ${typeof convert}`);
console.log('------------------------------------------');
console.log('string.toFloat()\n', convertToFloat);
console.log(`Type Data: ${typeof convertToFloat}`);

console.log('------------------------------------------');
const number = 10;
Number.prototype.toString = () => number + '';
console.log('number.toString()\n', number.toString());
console.log(`Type Data: ${typeof number.toString()}`);

Related

  1. toInt()
    String.prototype.toInt = function(){
      return parseInt(this);
    };
    
  2. toInt()
    String.prototype.toInt = function() {
      return parseInt(this, 10);
    };
    
  3. toInt()
    String.prototype.toInt = function() {
      return parseInt(this);
    
  4. toInt()
    String.prototype.toInt = function(){
        var str = this;
        str = str.toLowerCase();
        str = str.replace(/ /g, "");
        str = str.replace(/?/g, "");
        return parseInt(str);
    
  5. toInt(fallBack)
    String.prototype.toInt = function (fallBack) {
        fallBack = fallBack || 0;
        var str = this;
        if(!str){
            return fallBack;
        var strAsInt = parseInt(str);
        if(isNaN(strAsInt)){
            return fallBack;
    ...
    
  6. toIntZero()
    String.prototype.toIntZero = function(){
      var val = parseInt(this);
      if(isNaN(val)){
        return 0;
      }else{
        return val
    };
    
  7. toInteger()
    String.prototype.toInteger=function(){
      var s,x;
      if(s=/(^[+-]?)(0b[01]+$)/.exec(this)) {
        x=Number(s[2]);
        return s[1]!='-'?x:-x;
      } else if(s=/(^[+-]?)(0o[0-7]+$)/.exec(this)) {
        x=Number(s[2]);
        return s[1]!='-'?x:-x;
      } else if(s=/(^[+-]?)(0x[0-9a-fA-F]+$)/.exec(this)) {
    ...