Created
March 8, 2024 14:21
-
-
Save HeySreelal/b738740b7e2ec24b50cfee3abadf06fc to your computer and use it in GitHub Desktop.
Yeah, just run it and see how large a billion actually is!
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() { | |
final million = 1e6; | |
final billion = 1e9; | |
final mDur = Duration(seconds: million.toInt()); | |
final bDur = Duration(seconds: billion.toInt()); | |
print('1 million seconds is\n${durToString(mDur)}'); | |
print(""); | |
print('1 billion seconds is\n${durToString(bDur)}'); | |
} | |
String durToString(Duration d) { | |
// A Years, B Months, C Days, D Hours, E Minutes, F Seconds | |
final years = d.inDays ~/ 365; | |
final months = (d.inDays % 365) ~/ 30; | |
final days = (d.inDays % 365) % 30; | |
final hours = d.inHours % 24; | |
final minutes = d.inMinutes % 60; | |
final seconds = d.inSeconds % 60; | |
final yStr = years > 0 ? '$years years' : ''; | |
final mStr = months > 0 ? '$months months' : ''; | |
final dStr = days > 0 ? '$days days' : ''; | |
final hStr = hours > 0 ? '$hours hours' : ''; | |
final minStr = minutes > 0 ? '$minutes minutes' : ''; | |
final secStr = seconds > 0 ? '$seconds seconds' : ''; | |
final parts = [yStr, mStr, dStr, hStr, minStr, secStr]; | |
final nonEmptyParts = parts.where((p) => p.isNotEmpty).toList(); | |
if (nonEmptyParts.length == 1) { | |
return nonEmptyParts.first; | |
} | |
final lastPart = nonEmptyParts.removeLast(); | |
return '${nonEmptyParts.join(', ')} and $lastPart'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Run it, and then you'll see:
That's it.