首页 > 其他分享 >【转】Typescript: how to convert number to an int8, int16, int32, uint8, uint16, or uint32

【转】Typescript: how to convert number to an int8, int16, int32, uint8, uint16, or uint32

时间:2022-11-15 11:44:49浏览次数:68  
标签:int32 convert Typescript return val number sign let result

转载:Typescript: how to convert number to an int8, int16, int32, uint8, uint16, or uint32

export class CONVERT
{
   static NumberToUint32(x:number):number 
   {
      return x >>> 0;
   }
   static NumberToUint16(x:number):number 
   {
      return this.NumberToUint32(x) & 0xFFFF;
   }
   static NumberToUint8(x:number):number 
   {
      return this.NumberToUint32(x) & 0xFF;
   }

   static NumberToInt32(x:number): number
   {
      return x >> 0;
   }
   static NumberToInt16(x:number): number
   {
      let r: number = 0; 
      let n = this.NumberToUint16(x);
      if(n & 0x8000)
         r =  0xFFFF8000|(n&0x7FFF);
      else 
         r = n;         
      return(r);      
   }
   static NumberToInt8(x:number): number
   {      
      let r: number = 0; 
      let n = this.NumberToUint8(x);
      if(n & 0x80)
         r =  0xFFFFFF80|(n&0x7F); 
      else 
         r = n;     
      return(r);      
   }      

   static StrToNumber(val: string, defaultVal:number = 0): number
   {        
      let result:number = defaultVal;      
      if(val == null) 
         return result;            
      if(val.length == 0) 
         return result;      
      val = val.trim();
      if(val.length == 0) 
         return(result);
      let sign:number = 1;     
      //
      // . obtain sign from string, and place result in "sign" local varible. The Sign naturally defaults to positive
      //     1 for positive, -1 for negative.
      // . remove sign character from val. 
      //      Note, before the function returns, the result is multiplied by the sign local variable to reflect the sign.
      // . error check for multiple sign characters
      // . error check to make sure sign character is at the head or tail of the string
      //              
      {  
         let positiveSignIndex = val.indexOf('+');
         let negativeSignIndex = val.indexOf('-');
         let nTailIndex = val.length-1;
         //
         // make sure both negative and positive signs are not in the string
         //
         if( (positiveSignIndex != -1) && (negativeSignIndex != -1) ) 
             return result;
         //
         // handle postive sign
         //
         if (positiveSignIndex != -1)
         {
            //
            // make sure there is only one sign character
            //
            if( (positiveSignIndex != val.lastIndexOf('+')) )
             return result;     
             //
             // make sure the sign is at the head or tail
             //
             if( (positiveSignIndex > 0) && (positiveSignIndex < nTailIndex )  )
                 return result;
             //
             // remove sign from string
             //
             val = val.replace("+","").trim();                 
         }    
         //
         // handle negative sign
         //
         if (negativeSignIndex != -1)
         {
            //
            // make sure there is only one sign character
            //
            if( (negativeSignIndex != val.lastIndexOf('-')) )
             return result;     
             //
             // make sure the sign is at the head or tail
             //
             if( (negativeSignIndex > 0) && (negativeSignIndex < nTailIndex )  )
                 return result;
             //
             // remove sign from string
             //
             val = val.replace("-","").trim();  
             sign = -1;                 
         }               
         //
         // make sure text length is greater than 0
         //       
         if(val.length == 0) 
            return result;                             
      }   
      //
      // convert string to a number
      //
      var r = +(<any>val);
      //
      // apply sign if no errors
      //
      if( (r != null) && (!isNaN(r)) )
      {          
         result = r*sign;         
      }
      return(result);    
   }
}

 

标签:int32,convert,Typescript,return,val,number,sign,let,result
From: https://www.cnblogs.com/ZXdeveloper/p/16891924.html

相关文章

  • 取消TypeScript的强类型校验
    作用取消TypeScript的强类型校验例在TypeScript导入echarts时,无法识别;报文件“*/utils/lib/ec-canvas/echarts.js”不是模块使用//@ts-ignore解决思路如下//@......
  • TypeScript 使用 echart 小结
    可使用封装好的echarts-for-weixin下载说明引入组件说明导入组件{"usingComponents":{"ec-canvas":"../../ec-canvas/ec-canvas"}}ts/js引入echa......
  • 文件导入 - TypeScript
    require与importrequire:是运行时调用,所以require理论上可以运用在代码的任何地方import:是编译时调用,所以必须放在文件开头-使用require导入index.ts//----------......
  • [Typescript] 102. Hard - String to Number
    Convertastringliteraltoanumber,whichbehaveslike Number.parseInt. /*_____________YourCodeHere_____________*/typeToNumber<Sextendsstring,......
  • [Typescript] 101. Hard - Typed Get
    The get functioninlodash isaquiteconvenienthelperforaccessingnestedvaluesinJavaScript.However,whenwecometoTypeScript,usingfunctionslike......
  • TypeScript系列 -> 遇到报错 Cannot find name ‘console‘. Do you need to change y
    学习ts遇到的报错Cannotfindname‘console‘.Doyouneedtochangeyourtargetlibrary?gingthe‘lib‘compileroption解决办法:需要安装TypeScript助手的运......
  • [Typescript] 100. Hard - IsAny
    Sometimesit'susefultodetectifyouhaveavaluewith any type.Thisisespeciallyhelpfulwhileworkingwiththird-partyTypescriptmodules,whichcanex......
  • Sql 中常用日期转换Convert(Datetime)
    CONVERT(data_type,expression[,style]) convert(varchar(10),字段名,转换格式)说明:此样式一般在时间类型(datetime,smalldatetime)与字符串类型(nchar,nvarchar,char,v......
  • [Typescript] 99. Hard - CamelCase
    Implement CamelCase<T> whichconverts snake_case stringto camelCase.ForexampletypecamelCase1=CamelCase<'hello_world_with_types'>//expectedtobe......
  • [Typescript] 98. Medium - Append to object
    Implementatypethataddsanewfieldtotheinterface.Thetypetakesthethreearguments.Theoutputshouldbeanobjectwiththenewfield.Forexamplety......