从零开始前端学习[55]:类型转换和Math方法

类型转换和Math方法

  1. 类型转换(显示类型转换,隐式类型转换)
  2. Math方法

提示:
博主:章飞_906285288
博客地址:http://blog.csdn.net/qq_29924041


类型转换(显示类型转换,隐式类型转换)

我们知道在js中的检测类型方式:typeof 要转换的变量或者数据类型,返回一个类型名称的字符串
而其中显式类型转换,其实就是通过js中的一些方法(函数)转换,如parseInt,parseFloat等

显示转换

显示转换有时候常用的一些转换函数:parseInt,parseFloat,toString等类型。


<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Titletitle>
  <meta charset="UTF-8">
  <meta name="Generator" content="EditPlus®">   
  <meta name="Author" content="作者是谁">       
  <meta name="Keywords" content="关键词">
  <meta name="Description" content="描述和简介">
  <style type="text/css">                                        
        body,dl,dd,dt,p,h1,h2,h3,h4,h5,h6{ margin: 0;}
        ul,ol{margin: 0; list-style: none; padding: 0;}
        a{ text-decoration: none; }
        *{ margin: 0; padding: 0; }
  style>
head>
<body>
<script>
  var demo_int = 10;   //定义一个整形数据
  console.log("demo_int:"+ demo_int);
  console.log("demo_int type:"+ typeof demo_int);

  var demo_string_width = "110.05px"
  console.log("demo_string_width:"+demo_string_width);
  console.log("demo_string_width type:"+typeof demo_string_width);
  console.log("parseInt demo_string_width:"+ parseInt(demo_string_width));  //从字符串类型中过滤出整形
  console.log("parseInt demo_string_width _2:"+ parseInt(demo_string_width,2)); //以二进制形式读取
  console.log("parseInt demo_string_width _16:"+ parseInt(demo_string_width,16));//以16进制形式输出
  console.log("parseInt demo_string_width:"+ parseFloat(demo_string_width)); //从字符串类型中过滤出实形
  var demo_string = "aaasd110.05px"
  console.log("parseInt demo_string:"+ parseInt(demo_string));  //从字符串类型中过滤出整形
  console.log("parseInt demo_string:"+ parseFloat(demo_string));  //从字符串类型中过滤出整形

  var demo_Int2 = 100;
  console.log(typeof  demo_Int2.toString());  //将整型数据转换为字符串型
  console.log(demo_Int2.toString()+"1234");  //将整型数据转为字符串型后,在与数据相加得到一个字符串
script>
body>
html>

上述实测试代码:测试parseInt,parseFloat以及toString类型的使用

从零开始前端学习[55]:类型转换和Math方法_第1张图片

隐式转换

隐式类型转换,就是在没有任何显示转换的基础上,通过运算符等操作,由浏览器自动为我们进行这样的一种类型转换的过程,其实这种转换自古其实就有,不管在java或者c/C++中都是有的,比如一个整型与实型的相加,乘除等等。这个时候都是会由系统自动去进行一系列的类型转换过程。

1:+ 全为数字类型则为运算,有字符串则为拼接 从左到右计算
2:- ,* , / ,% 只要有一边是数字就将结果直接转换为数字类型
3:对于boolean类型的加减都是按照数字类型来进行的


<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Titletitle>
  <meta charset="UTF-8">
  <meta name="Generator" content="EditPlus®">   
  <meta name="Author" content="作者是谁">       
  <meta name="Keywords" content="关键词">
  <meta name="Description" content="描述和简介">
  <style type="text/css">                                        
        body,dl,dd,dt,p,h1,h2,h3,h4,h5,h6{ margin: 0;}
        ul,ol{margin: 0; list-style: none; padding: 0;}
        a{ text-decoration: none; }
        *{ margin: 0; padding: 0; }
  style>
head>
<body>
<script>
  //如果加号左右全是数字,就是按照正常数字的形式进行加减
  var allNumberSum = 1 + 2;
  console.log(allNumberSum);
  console.log(typeof allNumberSum);

  //如果其中都是数字类型字符串,如果从左到右数字的话,会先加,遇到字符串就直接拼接成字符串
  var sideofString = 1 + 2 +"3"+"5";
  console.log(sideofString);
  console.log(typeof sideofString);

  //如果其中有数字类型字符串,如果先是字符串,后面是数字的话,会直接转换成字符串
  var sideofString2 ="bbb" + 1 + 3 + "3"+"aaaa";
  console.log(sideofString2);
  console.log(typeof sideofString2);

//  boolean值会被强制转换为数字,true被转为1,false转为0
  var booleanResult = true + false;
  console.log(booleanResult);

//  不是数字类型的字符串 或者   其他类型的运算,结果是NaN(除开加法运算,因为是拼接)不是数字类型的字符串
  var str2 = "str2";
  var sum7 = str2 - 1;
  var str3 = undefined;
  console.log(sum7); //NaN
  console.log(str3 - str2);
script>
body>
html>

注意:隐式类型转换的注意事项主要写在了上述的备注上面
从零开始前端学习[55]:类型转换和Math方法_第2张图片


Math方法

在任何一门已知的语言中,数学函数Math库的调用都是及其重要的,在js里面当然也是一样的。其调用的形式跟java里面也类似,静态方法的一种调用方法。
相关函数如下所示:

函数 函数意义
Math.ceil() 向上取整
Math.floor() 向下取整
Math.round() 四舍五入
Math.abs() 取绝对值
Math.random() 默认是取从[0,1)之间的随机数
Math.max(a,b) 取最大值 里面个数不限 非数字字符串识别不了
Math.min(a,b) 取最大值 里面个数不限 非数字字符串识别不了
Math.sqrt(x) x的平方根
Math.pow(x,y) x的y次幂

基本上常规用的也就上面这些了,调用都很简单。

你可能感兴趣的:(从零开始挑战前端,约战前端)