Created
June 1, 2022 20:03
-
-
Save hawkkiller/052d675ed5bd5d833d2bbd69671c00a5 to your computer and use it in GitHub Desktop.
Format by digits
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
returns 10.000, 9.999, 12.000, 12.000.000