How do you round a double in Dart to a given degree of precision AFTER the decimal point?

前端 未结 12 1055
[愿得一人]
[愿得一人] 2020-12-08 03:46

Given a double, I want to round it to a given number of points of precision after the decimal point, similar to PHP\'s round() function.

The closest thing I

12条回答
  •  情歌与酒
    2020-12-08 04:17

    You can use toStringAsFixed in order to display the limited digits after decimal points. toStringAsFixed returns a decimal-point string-representation. toStringAsFixed accepts an argument called fraction Digits which is how many digits after decimal we want to display. Here is how to use it.

    double pi = 3.1415926;
    const val = pi.toStringAsFixed(2); // 3.14
    

提交回复
热议问题