Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Counterintuitive result in NumberFormat of Intl Package in Dart/Flutter

Why does NumberFormat(".##").format(17.46) leads to a string of 17.46 and not .46?

How can I achieve the latter, i.e. remove all digits in front of the decimal sign?

like image 593
Heikkisorsa Avatar asked Jan 20 '26 20:01

Heikkisorsa


1 Answers

The NumberFormat only changes the way that a number is being displayed(basically, what formatting is). So you can't get the fractional part of the number(it doesn't work like pattern matching). Instead, you can use:

var num = 17.46;
var fraction = num.toString().split('.')[1];

Note: you can use '.' + num.toString().split('.')[1] to get the fraction part with the starting dot.

You can read more about the ICU Formatting that NumberFormat uses in this link.

like image 74
Mobina Avatar answered Jan 23 '26 10:01

Mobina



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!