英文:
How to display Duration depending on it's value
问题
以下是翻译好的部分:
例如,我有一些以下的 Duration
值:
0:12:15.000000 // 小时为 0,所以我想显示为 mm-ss => 12:15
0:01:02.000000 // 分钟小于 10,所以我想显示为 m-ss => 1:02
0:00:05.000000 // 分钟为 0,所以我想显示为 m-ss => 0:05
我从视频信息中导入这些 Duration 值,所以我不知道 .000000
的类型是什么,但我也想将它们去掉。
我应该如何最好地处理这些情况
英文:
For example i have some of the following Duration
values
0:12:15.000000 // the hour is 0 so i want to display the value like mm-ss => 12:15
0:01:02.000000 // the minutes is less than 10 so i want to display the value like m-ss => 1:02
0:00:05.000000 // the minutes is 0 so i want to the value like m-ss => 0:05
i import these Duration values from video info so i don't know what was the type of .000000
is . but i want to remove them too .
How could i handle these scenarios in best way
答案1
得分: 1
以下是您提供的代码的中文翻译:
我猜这可能会有所帮助
String formatDuration(Duration duration) {
String twoDigitMinutes = duration.inMinutes.remainder(60).toString().padLeft(2, '0');
String twoDigitSeconds = duration.inSeconds.remainder(60).toString().padLeft(2, '0');
if (duration.inHours == 0) {
return '$twoDigitMinutes:$twoDigitSeconds';
} else if (duration.inMinutes < 10) {
return '${duration.inHours}:$twoDigitMinutes:$twoDigitSeconds';
} else {
return '${duration.inHours}:$twoDigitMinutes:$twoDigitSeconds';
}
}
Duration duration = Duration(hours: 0, minutes: 12, seconds: 15);
String formattedDuration = formatDuration(duration);
print(formattedDuration); // 打印 "12:15";
duration = Duration(hours: 0, minutes: 1, seconds: 2);
formattedDuration = formatDuration(duration);
print(formattedDuration); // 打印 "1:02";
duration = Duration(hours: 0, minutes: 0, seconds: 5);
formattedDuration = formatDuration(duration);
print(formattedDuration); // 打印 "0:05";
希望这对您有所帮助。
英文:
I guess this might help
String formatDuration(Duration duration) {
String twoDigitMinutes = duration.inMinutes.remainder(60).toString().padLeft(2, '0');
String twoDigitSeconds = duration.inSeconds.remainder(60).toString().padLeft(2, '0');
if (duration.inHours == 0) {
return '$twoDigitMinutes:$twoDigitSeconds';
} else if (duration.inMinutes < 10) {
return '${duration.inHours}:$twoDigitMinutes:$twoDigitSeconds';
} else {
return '${duration.inHours}:$twoDigitMinutes:$twoDigitSeconds';
}
}
Duration duration = Duration(hours: 0, minutes: 12, seconds: 15);
String formattedDuration = formatDuration(duration);
print(formattedDuration); // prints "12:15"
duration = Duration(hours: 0, minutes: 1, seconds: 2);
formattedDuration = formatDuration(duration);
print(formattedDuration); // prints "1:02"
duration = Duration(hours: 0, minutes: 0, seconds: 5);
formattedDuration = formatDuration(duration);
print(formattedDuration); // prints "0:05"
答案2
得分: -1
以下是翻译好的代码部分:
假设您有一个持续时间对象。您可以定义您的格式化函数如下并使用它。
String _formatVideoDuration(Duration duration) {
final hours = (duration.inHours);
final minutes = (duration.inMinutes % 60);
final ss = (duration.inSeconds % 60).toString().padLeft(2, '0');
if (hours > 9) {
return '${hours.toString().padLeft(2, '0')}:${minutes.toString().padLeft(2, '0')}:$ss';
} else if (hours > 0) {
return '${hours.toString().padLeft(1, '0')}:${minutes.toString().padLeft(2, '0')}:$ss';
} else if (minutes > 9) {
return '${minutes.toString().padLeft(2, '0')}:$ss';
} else {
return '${minutes.toString().padLeft(1, '0')}:$ss';
}
}
英文:
Assuming you have duration object. You can define your format function something like this and use it.
String _formatVideoDuration(Duration duration) {
final hours = (duration.inHours);
final minutes = (duration.inMinutes % 60);
final ss = (duration.inSeconds % 60).toString().padLeft(2, '0');
if (hours > 9) {
return '${hours.toString().padLeft(2, '0')}:${minutes.toString().padLeft(2, '0')}:$ss';
} else if (hours > 0) {
return '${hours.toString().padLeft(1, '0')}:${minutes.toString().padLeft(2, '0')}:$ss';
} else if (minutes > 9) {
return '${minutes.toString().padLeft(2, '0')}:$ss';
} else {
return '${minutes.toString().padLeft(1, '0')}:$ss';
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论