The rule is if value >= 500,000 it will be rounded up to 1,000,000, if the value < 500,000 it will be rounded down to 000,000
Here an example, if I have value like 4,843,820,00 it will be rounded up to 4,844,000,000
If I have value like 1,136,362,500 it will be rounded down to 1,136,000,000
If I have value like 1,500,000 will be rounded up to 2,000,000 & if I have like 1,450,000 it will be rounded down to 1,000,000
Here is what I tried
String kmbGenerator(number) {
if (number > 999 && number < 99999) {
int resulta = (number / 1000).round();
return "$resulta,000";
} else if (number > 99999 && number < 999999) {
int resulta = (number / 1000).round();
return '${resulta.toStringAsFixed(0)},000';
} else if (number > 999999 && number < 999999999) {
int resulta = (number / 1000000).round();
return "$resulta,000,000";
} else if (number > 999999999) {
int resulta = (number / 1000000000).round();
return "$resulta,000,000,000";
} else {
return number.toString();
}
}
2
Answers
Divide by one million, round, then multiply by one million:
Output:
Before you format round it using
num.round()
for example if you want to round to millions :you can either multiply it by million and format it or just convert to
String
and add',000,000'