Dart - NumberFormat

后端 未结 7 1012
北荒
北荒 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:50

    This will work.

    main() {
      double n1 = 15.00;
      double n2 = 15.50; 
    
      print(_formatDecimal(n1));
      print(_formatDecimal(n2));
    }
    
    _formatDecimal(double value) {
      if (value % 1 == 0) return value.toStringAsFixed(0).toString();
      return value.toString();
    }
    

    Output:

    15
    15.5
    

提交回复
热议问题