/// https://stackoverflow.com/a/9462382 nFormatter(dynamic num, [int digits]) { var si = [ {'value': 1, 'symbol': ''}, {'value': 1e3, 'symbol': 'k'}, {'value': 1e6, 'symbol': 'M'}, {'value': 1e9, 'symbol': 'G'}, {'value': 1e12, 'symbol': 'T'}, {'value': 1e15, 'symbol': 'P'}, {'value': 1e18, 'symbol': 'E'}, ]; var rx = RegExp(r'\.0+$|(\.[0-9]*[1-9])0+$'); var i; for (i = si.length - 1; i > 0; i--) { if (num.abs() >= si[i]['value']) { break; } } return (num / si[i]['value']).toStringAsFixed(digits).replaceAll(rx, '') + si[i]['symbol']; } }
来源:https://www.cnblogs.com/teemwu/p/12640349.html