Dart - NumberFormat

后端 未结 7 1011
北荒
北荒 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:44

    Not very easily. Interpreting what you want as printing zero decimal places if it's an integer value and precisely two if it's a float, you could do

    var forInts = new NumberFormat();
    var forFractions = new NumberFormat();
    
    forFractions.minimumFractionDigits = 2;
    forFractions.maximumFractionDigits = 2;
    
    format(num n) => 
        n == n.truncate() ? forInts.format(n) : forFractions.format(n);
    
    print(format(15.50));
    print(format(15.0));
    

    But there's little advantage in using NumberFormat for this unless you want the result to print differently for different locales.

提交回复
热议问题