dart 内置数据类型 Number

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/coderinchina/article/details/91430698

dart支持如下几种数据类型

The Dart language has special support for the following types:

  • numbers
  • strings
  • booleans
  • lists (also known as arrays)
  • sets
  • maps
  • runes (for expressing Unicode characters in a string)
  • symbols
  1. Number类型

number分为二种,一种是int  一种是double

看来自官网的解释:

int
Integer values no larger than 64 bits, depending on the platform. On the Dart VM, values can be from -263 to 263 - 1. Dart that’s compiled to JavaScript uses JavaScript numbers, allowing values from -253 to 253 - 1.

例子:

void main(){
  int x = 10;
  print(x);
}

我们点击int 点击int 去看它的源代码:

abstract class int extends num 

发现它是继承了num对象

我们可以看下int 给我们提供了哪些方法:

我们练习几个常用的方法:

1:isEven  这是表示这个数是不是偶数

void main(){
  int x = 18;
  print(x.isEven);
}

2:isOdd 判断是否是奇数

void main(){
  int x = 19;
  print(x.isOdd);
}

3:toDouble  这是int转double

void main(){
  int x = 3;
  print(x.toDouble());
}

返回结果是3.0

4:abs()方法表示绝对值  

round()四舍五入

floor()此整数所需的最小位数

ceil():返回此不小于的最小整数  比如3.1。返回的是4

truncate():返回去掉小数点后面的整数

现在介绍下double  其实上面的例子中我们已经使用到了double

double
64-bit (double-precision) floating-point numbers, as specified by the IEEE 754 standard

翻译:

64位(双精度)浮点数,由IEEE 754标准指定

double 和int 都是num的子类  

整数类型注意二个判断

isNan:是否是数值类型.这个和JavaScript一样,看下它的源码注释

/** True if the number is the double Not-a-Number value; otherwise, false. */

例子:

void main(){
 int x = 0;
 int y = 0;
  print((x/y).isNaN);
}

就是0/0这种情况在数学上是分母不能为0 在dart表示为 NaN

isInfinite:

当数为正无穷或负无穷时为真;否则,假

void main(){
 int x = 1;
 int y = 0;
  print((x/y).isInfinite);
}

猜你喜欢

转载自blog.csdn.net/coderinchina/article/details/91430698