Dart - NumberFormat

后端 未结 7 1007
北荒
北荒 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';
    }
    
    0 讨论(0)
提交回复
热议问题