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

前端 未结 12 1069
[愿得一人]
[愿得一人] 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:07

    num.toStringAsFixed() rounds. This one turns you num (n) into a string with the number of decimals you want (2), and then parses it back to your num in one sweet line of code:

    n = num.parse(n.toStringAsFixed(2));
    

提交回复
热议问题