Skip to content

Instantly share code, notes, and snippets.

@hawkkiller
Created June 1, 2022 20:03
Show Gist options
  • Save hawkkiller/052d675ed5bd5d833d2bbd69671c00a5 to your computer and use it in GitHub Desktop.
Save hawkkiller/052d675ed5bd5d833d2bbd69671c00a5 to your computer and use it in GitHub Desktop.
Format by digits
void main() {
print(formatByDigits(10000));
print(formatByDigits(9999));
print(formatByDigits(12000));
print(formatByDigits(12000000));
}
String formatByDigits(int number) {
final s = number.toString().split('');
return s.reversed
.mapIndexed(
(i, e) sync* {
if (e % 3 == 0 && e != 0) yield '.';
yield i;
},
)
.toList()
.reversed
.join('');
}
extension Abobus<T> on Iterable<T> {
Iterable<K> mapIndexed<K>(Iterable<K> Function(T, int) fn) sync* {
final l = toList();
for (var i = 0; i < length; i++) {
yield* fn(l[i], i);
}
}
}
@hawkkiller
Copy link
Author

returns 10.000, 9.999, 12.000, 12.000.000

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment