用飞镖将秒转换为mm:ss [英] Convert seconds to mm:ss in dart

查看:46
本文介绍了用飞镖将秒转换为mm:ss的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将HH:SS格式的秒秒转换为分钟和秒,但是我的逻辑没有给我我想要的东西.

I want to convert seconds to minutes and seconds in HH:SS format but my logic don't give me what I want.

  static double toMin (var value){
            return (value/60);
          }

我还希望能够将余数添加到分钟值,因为有时我的部门给我的秒值大于60,这是不准确的

I also want to be able to add the remainder to the minutes value because sometimes my division gives me a seconds value more that 60 which is not accurate

推荐答案

您可以尝试以下解决方案,这就是我的做法:

You could try the solution below, this is how I'm doing it:

  String formatHHMMSS(int seconds) {
    if (seconds != null && seconds != 0) {
      int hours = (seconds / 3600).truncate();
      seconds = (seconds % 3600).truncate();
      int minutes = (seconds / 60).truncate();

      String hoursStr = (hours).toString().padLeft(2, '0');
      String minutesStr = (minutes).toString().padLeft(2, '0');
      String secondsStr = (seconds % 60).toString().padLeft(2, '0');

      if (hours == 0) {
        return "$minutesStr:$secondsStr";
      }
      return "$hoursStr:$minutesStr:$secondsStr";
    } else {
      return "";
    }
  }

这篇关于用飞镖将秒转换为mm:ss的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆