Dart - NumberFormat

后端 未结 7 1033
北荒
北荒 2021-02-19 00:08

Is there a way with NumberFormat to display :

  • \'15\' if double value is 15.00
  • \'15.50\' if double value is 15.50

Thanks for yo

7条回答
  •  余生分开走
    2021-02-19 00:55

    A variant of double value formatting:

    void main (){
      final n1 = 15.00;
      final n2 = 15.50;
      print(format(n1));
      print(format(n2));
    }
    String format(double n) {
      final fraction = n - n.toInt();
      if (fraction == 0.0) {
        return n.toString();
      }
      var twoDigitFraction = (fraction * 100).truncateToDouble().toInt();
      return '${n.toInt()}.$twoDigitFraction';
    }
    

提交回复
热议问题