JS learning-forced data type conversion: Boolean(), Number(), parsetInt(), parsetFloat()

  1. Boolean() casts other data types into Boolean values

    Mantra: non-zero is true (a number is not 0, it is true when converted to a Boolean value) non-empty is true (a string is not empty, it is true when converted to a Boolean value)

    alert(Boolean(-100));//true
    alert(Boolean(90));//true
    alert(Boolean(0));//false

    alert(Boolean("jij"));//true
    alert(Boolean("uiuuig"));//true
    alert(Boolean(""));//false

    alert(Boolean(Infinity));//true
    alert(Boolean(NaN));//false
    alert(Boolean(undefined));//false
  1. Number() converts other data types to numbers

    Only numbers in the string can be converted into numbers, and all others are NaN

 alert(Number("100"));//100
 alert(Number("2a"));//NaN
  1. parsetInt()

① Arrangement

    alert(parseInt("3.14a"));//3
    alert(parseInt("3.14"));//3
    alert(parseInt("b3.14a"));//NaN  一开始就有字母
    alert(parseInt("100a"));//100


②To convert other hexadecimals to decimals, a string must be passed in (52: binary 110100; octal 64; hexadecimal 34)

   var str1 = "110100";
   alert(parseInt(str1,2));//52   第一个参数是字符串 第二个表示传入的数是什么进制
   var str2 = "64";
   alert(parseInt(str2,8));//52  
   var str3 = "34";
   alert(parseInt(str3,16));//52 
 
  1. parsetFloat()

Floating point number

   alert(parseFloat("3.14a"));//3.14

Guess you like

Origin blog.csdn.net/qq_43812504/article/details/108405504