Dart num常用函数介绍

2023-01-03  本文已影响0人  君顏

Dart num常用函数

  double a = 1.0;
  double b = 0.0;
  double c = -1.0;
  double d = -0.0;
  print('a.isNegative=>${a.isNegative}');
  print('b.isNegative=>${b.isNegative}');
  print('c.isNegative=>${c.isNegative}');
  print('d.isNegative=>${d.isNegative}');

//输出结果
a.isNegative=>false
b.isNegative=>false
c.isNegative=>true
d.isNegative=>true
  int a = 1;
  int b = 0;
  int c = -1;
  int d = -0;
  print('a.isNegative=>${a.isNegative}');
  print('b.isNegative=>${b.isNegative}');
  print('c.isNegative=>${c.isNegative}');
  print('d.isNegative=>${d.isNegative}');

//输出结果
a.isNegative=>false
b.isNegative=>false
c.isNegative=>true
d.isNegative=>false

总结: int 0与-0都返回false;double 0.0返回false,-0.0返回true



  print('${(-15).abs()}');
  print('${(0).abs()}');
  print('${(-0.0).abs()}');
  print('${(14).abs()}');

//输出结果
15
0
0.0
14

(3.5).round() = 4
(-3.5).round() = -4
(3.3).round() = 3
(-3.3).round()} = -3
(3.7).round() = 4
(-3.7).round()} = -4

总结:如果该值大于最高可表示正整数,则结果为最高正整数。如果该值小于最高可表示负整数,则结果为最高负整数。


(3.3).floor() = 3
(-3.3).floor()} = -4
(3.7).floor() = 3
(-3.7).floor()} = -4


(3.5).ceil() = 4
(-3.5).ceil() = -3
(3.3).ceil() = 4
(-3.3).ceil()} = -3
(3.7).ceil() = 4
(-3.7).ceil()} = -3


(3.5).truncate() = 3
(-3.5).truncate() = -3
(3.3).truncate() = 3
(-3.3).truncate()} = -3
(3.7).truncate() = 3
(-3.7).truncate()} = -3


(3.5).clamp(0, 5) = 3.5
(-3.5).clamp(0, 5) = 0
(0).clamp(0, 5) = 0

(3.56523).toStringAsFixed(2) = 3.57
(-3.56523).toStringAsFixed(2) = -3.57
(3).toStringAsFixed(2) = 3.00

Dart int 常用函数




2.toRadixString(2) => 10
255.toRadixString(16) => ff
255.toRadixString(8) => 377

上一篇 下一篇

猜你喜欢

热点阅读