如何在Dart中将double转换为int?

217
以下代码会产生以下错误:
int calc_ranks(ranks)
{
  double multiplier = .5;
  return multiplier * ranks;
}

方法calc_ranks定义的返回类型为double,而非int。我该如何将其四舍五入或强制转换为int

11个回答

264

使用round()方法进行四舍五入:

int calc_ranks(ranks) {
    double multiplier = .5;
    return (multiplier * ranks).round();
}

23
将其截断为整数:return (multiplier * ranks).toInt(); (请参见https://api.dartlang.org/docs/channels/stable/latest/dart_core/double.html) - Everton
9
我曾遇到过 (0.57 * 100).toInt() 解析为 56 的情况,所以大家要小心。 - 最白目

180

您可以使用以下任何一种。

double d = 20.5;

int i = d.toInt(); // i = 20
int i = d.round(); // i = 21
int i = d.ceil();  // i = 21
int i = d.floor(); // i = 20

2
那么,toInt和floor之间有什么区别?round和ceil呢? - Hector Aguero
3
floor 和 ceil 会将 double 转换为最接近的整数,分别向下取整和向上取整,因此 (20.1).floor() 为 20,(20.1).ceil() 为 21。toInt 则是截断小数部分,只保留整数部分,而 round 则使小数部分小于 0.5 的数字向下舍入,其他数字向上舍入。 - Kamil Poniewierski

92
你可以简单地使用toInt()num转换为int
int calc_ranks(ranks)
{
  double multiplier = .5;
  return (multiplier * ranks).toInt();
}

请注意,要完全执行相同的操作,您可以使用截断除法运算符

int calc_ranks(ranks) => ranks ~/ 2;

23

我看到很多答案,但描述较少。希望我的回答能够增加一些价值。 初始化变量,然后看看它如何随着不同的方法而改变。

 double x = 8.5;

toInt()

此方法截断小数值。

 int a = x.toInt();
 print(a); // 8

truncate()

它还会截断小数值。

 int b = x.truncate();
 print(b); // 8

round()

它返回离输入数字最近的整数。它使用“四舍五入”模式。

  int c = x.round();
  print(c); // 9

ceil()

它返回大于该值的最接近的整数。

  int c = x.ceil();
  print(c); // 9

floor()

该函数返回比参数小的最大整数。

  int c = x.floor();
  print(c); // 8


6

我看了上面的答案,并添加了更多的答案,使得内容更加易于理解。

double value = 10.5;

使用 toInt() 方法

void main(){
  double value = 10.5;
  var y = value.toInt();
  print(y);
  print(y.runtimeType);
}

使用 round() 方法

round() 方法返回最接近该双精度小数的整数。

void main(){
  double value = 9.6;
  var b = value.round();
  print(b);
  print(b.runtimeType);
}

使用 ceil()

ceil() 方法返回大于或等于给定 double 值的最小整数。

void main(){
  double value = 9.5;
  var d = value.ceil();
  print(d);
  print(d.runtimeType);
}

使用floor()

floor() 方法返回不大于给定双精度数的最大整数。

void main(){
  double value = 10.9;
  var j = value.floor();
  print(j);
  print(j.runtimeType);
}

结论

我们已经介绍了4种将Dart和Flutter中的双精度浮点数转换为整数的不同技术。您可以根据您的使用情况选择适合您的方法来解决问题。Flutter非常棒,并提供了很多令人惊艳的功能。


5
将double转换为int只需使用除法:
double01 ~/ double02

注:运算符 x ~/ y 比 (x / y).toInt() 更高效。

乘法、加法和减法

(double01 * double02).toInt
(double01 + double02).toInt
(double01 - double02).toInt

1
不是 ".toInt" 而是 ".toInt()"。 - André

2

很容易,

(20.8).round()

对于String类型,

double.tryParse(20.8).round()

1

从字符串转换为整数 ->

  1. 如果您的字符串是整数格式,例如'10' 则使用 ---->

    int.parse(value)

    但是如果字符串是双精度格式,例如'10.6' 则使用以下方式 ---->

    double.parse(value).toInt()

  2. 将双精度转换为整数

    doubleValue.toInt()


0

试试这个!

int calc_ranks(ranks)
{
  double multiplier = .5;
  return (multiplier * ranks).truncate();
}

0
    class CurrencyUtils{
    
      static int doubletoint(double doublee) {
        double multiplier = .5;
        return (multiplier * doublee).round();
      }
    }
    
    ----------------------
    
    
    CustomText( CurrencyUtils.doubletoint(
double.parse(projPageListss[0].budget.toString())
).toString(),
         fontSize: 20,
         color: Colors.white,
         font: Font.QuicksandSemiBold,
                                            ), 

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接