dartlang中的double.toStringAsFixed和toStringAsPrecision有什么区别? [英] What's the difference between double.toStringAsFixed and toStringAsPrecision in dartlang?

查看:835
本文介绍了dartlang中的double.toStringAsFixed和toStringAsPrecision有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道这两种方法之间的区别。我以为 toStringAsFixed 会修剪数字,但从doc中的示例来看,两者都是四舍五入。

I want know what's difference between these two methods. I thought toStringAsFixed trims the number but from the examples in doc, both are rounding the numbers.

这里是相关的问题: https://github.com/dart-lang/sdk/issues/25947

推荐答案

1。 Double.toStringAsPrecision(int)

1. Double.toStringAsPrecision(int)

转换 num 转换为 double ,并返回具有精确精确有效数字的 String 表示形式。

Converts a num to a double and returns a String representation with exactly precision significant digits.

参数precision必须是一个满足以下条件的整数: 1< = precision< = 21

The parameter precision must be an integer satisfying: 1 <= precision <= 21.

示例:

1.toStringAsPrecision(2);       // 1.0
1e15.toStringAsPrecision(3);    // 1.00e+15
1234567.toStringAsPrecision(3); // 1.23e+6
1234567.toStringAsPrecision(9); // 1234567.00
12345678901234567890.toStringAsPrecision(20); // 12345678901234567168
12345678901234567890.toStringAsPrecision(14); // 1.2345678901235e+19
0.00000012345.toStringAsPrecision(15); // 1.23450000000000e-7
0.0000012345.toStringAsPrecision(15);  // 0.00000123450000000000






2。 Double.toStringAsFixed(int)


2. Double.toStringAsFixed(int)

它也将数字四舍五入,但是在小数点后并返回根据您提供的 int 值得出的结果。

It also rounds the number but after the decimal place and return the result according to the int value you provide.

double d = 1.59;
String fixed1 = d.toStringAsFixed(1); // 1.6
String fixed2 = d.toStringAsFixed(2); // 1.59
String fixed3 = d.toStringAsFixed(3); // 1.590
String fixed4 = d.toStringAsFixed(4); // 1.5900

这篇关于dartlang中的double.toStringAsFixed和toStringAsPrecision有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆