round duration to closer second, for a countdown clock - https://dartpad.dartlang.org/2a08161c5f889e018938316237c0e810
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() { | |
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