Skip to content

Instantly share code, notes, and snippets.

@miketheman

miketheman/main.dart

Last active Jan 18, 2020
Embed
What would you like to do?
round duration to closer second, for a countdown clock - https://dartpad.dartlang.org/2a08161c5f889e018938316237c0e810
void main() {
Duration duration = new Duration(minutes: 1, seconds: 59, milliseconds: 99);
print(secondRounder(duration));
}
// Rounds Duration to seconds based on milliseconds
// This is semi-equivalent to a non-existing `Duration.ceil(milliseconds)`
Duration secondRounder(Duration duration) {
int roundedDuration;
if (duration.inMilliseconds > (duration.inSeconds * 1000)) {
roundedDuration = duration.inSeconds + 1;
} else {
roundedDuration = duration.inSeconds;
}
return new Duration(seconds: roundedDuration);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment