美文网首页
Dart num常用函数介绍

Dart num常用函数介绍

作者: 君顏 | 来源:发表于2023-01-03 14:08 被阅读0次

Dart num常用函数

  • isNegative 此数是否是一个负数
  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


  • sign 此数的符号 正1;负1;0

  • abs() 此数字的绝对值
  print('${(-15).abs()}');
  print('${(0).abs()}');
  print('${(-0.0).abs()}');
  print('${(14).abs()}');

//输出结果
15
0
0.0
14

  • round() 最接近此数字的整数(roundToDouble() 返回值是double)
(3.5).round() = 4
(-3.5).round() = -4
(3.3).round() = 3
(-3.3).round()} = -3
(3.7).round() = 4
(-3.7).round()} = -4

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


  • floor() 不大于此数字的最大整数 (floorToDouble()返回值是double)
(3.3).floor() = 3
(-3.3).floor()} = -4
(3.7).floor() = 3
(-3.7).floor()} = -4


  • ceil() 不小于此数字的最小整数(ceilToDouble()返回值是double)
(3.5).ceil() = 4
(-3.5).ceil() = -3
(3.3).ceil() = 4
(-3.3).ceil()} = -3
(3.7).ceil() = 4
(-3.7).ceil()} = -3


  • truncate() 截掉小数部分获得整数(truncateToDouble()返回值是double)
(3.5).truncate() = 3
(-3.5).truncate() = -3
(3.3).truncate() = 3
(-3.3).truncate()} = -3
(3.7).truncate() = 3
(-3.7).truncate()} = -3


  • clamp(num lowerLimit, num upperLimit) 返回此数字,使其处于[lowerLimit]-[upperLimit]范围内。
(3.5).clamp(0, 5) = 3.5
(-3.5).clamp(0, 5) = 0
(0).clamp(0, 5) = 0

  • toStringAsFixed(int fractionDigits) 保留[fractionDigits]位小数,小数最后一位四舍五入得
(3.56523).toStringAsFixed(2) = 3.57
(-3.56523).toStringAsFixed(2) = -3.57
(3).toStringAsFixed(2) = 3.00

Dart int 常用函数

  • gcd(int other) 此数字与[other]的最大公约数

  • isEven 是否为偶数

  • isOdd 是否为奇数

  • toRadixString(int radix) 以[radix]进制输出此数字
2.toRadixString(2) => 10
255.toRadixString(16) => ff
255.toRadixString(8) => 377

相关文章

  • dart基础

    [TOC] 主要介绍dart的一些语言规范和语法。 dart入口函数 变量 dart可以通过var来定义变量,可以...

  • Flutter-Dart构造函数介绍

    写flutter 真的是}})))}}})))改到崩溃。。。。 【转】Dart构造函数介绍 介绍 因为Flutte...

  • Dart构造函数介绍

    介绍 因为Flutter和Dart语言非常容易上手,很多学习Flutter的同学,一般都会直接写Flutter页面...

  • flutter 坐标转化 bd09-wg84 等

    import 'dart:math'; class GpsUtil {static const num pi = ...

  • Python自学——“增删改查”、深浅拷贝

    Python自学——“增删改查”、深浅拷贝 字符串常用函数 `split(str="", num=string.c...

  • 九月十号

    函数递归,方法一 $num=1; function test($num){ $num++; if($num<5){...

  • 三. Dart语法学习2

    Dart基本类型常用Api介绍 布尔类型 Dart中布尔类型很简单。如C语言中用bool声明,对应true & f...

  • dart:core - 数字

    // dart:core 核心库定义了num, int 以及double、collections、errors、...

  • 数字(num, int, double) Dart语言相较于其他语言,它只有三种数字类型,num, int, do...

  • 统计函数(Subtotal,countif,countifs)2

    一、统计函数(Subtotal) 1.基本用法:语法介绍:SUBTOTAL(function_num,ref1,…...

网友评论

      本文标题:Dart num常用函数介绍

      本文链接:https://www.haomeiwen.com/subject/umrlcdtx.html