倒计时的秒数格式化

huangapple go评论65阅读模式
英文:

Format seconds for countdown

问题

我正在开发一个需要在倒计时中以以下格式格式化秒数的功能(从60秒开始):

60.9、60.8、60.7、...、59.9、59.8 等等。

我编写了这段代码,除了60秒时出现问题外,其他情况都正常工作。当倒计时为60秒时,它会打印出00.9、00.8、00.7等,而当秒数减少到59时,它就正常工作了。

以下是代码:

extension RaceTimeFormatterService {

    /// This func print like 59.9, 59.8, etc.
    func formatForMsTimeElapsed(totalSeconds: Double) -> String {
        let (seconds, milliseconds) = divmod(totalSeconds * 1000, 1000)
        
        let (minutes, secondsLeft) = divmod(seconds, 60)
        
        let formatter = DateFormatter()
        formatter.dateFormat = "ss.S"
        let formattedTime = formatter.string(from: Date(timeIntervalSinceReferenceDate: TimeInterval(minutes * 60 + secondsLeft + milliseconds / 1000)))
        return formattedTime
    }
}

extension RaceTimeFormatterService {
    
    private func divmod(_ x: Double, _ y: Double) -> (Double, Double) {
        return (floor(x / y), x.truncatingRemainder(dividingBy: y))
    }
}

有没有办法以正确的方式来做这个,而不会在秒数为60时得到00.9?

英文:

I'm developing a feature that need to format seconds in this form in countdown (begin from 60s):

60.9, 60.8, 60.7,...,59.9, 59.8 etc.

I write this code and is working fine except for 60. When the countdown have 60 sec this print 00.9, 00.8, 00.7, etc. and when goes to 59, work well.

This is the code:

extension RaceTimeFormatterService {

    /// This func print like 59.9, 59.8, etc.
    func formatForMsTimeElapsed(totalSeconds: Double) -> String {
        let (seconds, milliseconds) = divmod(totalSeconds * 1000, 1000)
        
        let (minutes, secondsLeft) = divmod(seconds, 60)
        
        let formatter = DateFormatter()
        formatter.dateFormat = "ss.S"
        let formattedTime = formatter.string(from: Date(timeIntervalSinceReferenceDate: TimeInterval(minutes * 60 + secondsLeft + milliseconds / 1000)))
        return formattedTime
    }
}

extension RaceTimeFormatterService {
    
    private func divmod(_ x: Double, _ y: Double) -> (Double, Double) {
        return (floor(x / y), x.truncatingRemainder(dividingBy: y))
    }
}

Any idea of how to do this in a correct way and don't get 00.9 when seconds is 60 ?

答案1

得分: 1

不要让这与日期和时间有关,这是一个数字,所以最好使用 NumberFormatter 进行格式化。

func formatForMsTimeElapsed(totalSeconds: Double) -> String {
    let formatter = NumberFormatter()
    formatter.maximumFractionDigits = 1
    formatter.minimumFractionDigits = 1
    //formatter... 其他配置,比如舍入模式,如果相关的话
    return formatter.string(for: totalSeconds)!
}

更好的做法是将格式化器声明为函数外部的静态属性,这样就不需要为每次调用创建它。

英文:

Don't make this about date and time, it's a number so use a NumberFormatter instead to do the formatting

func formatForMsTimeElapsed(totalSeconds: Double) -> String {
    let formatter = NumberFormatter()
    formatter.maximumFractionDigits = 1
    formatter.minimumFractionDigits = 1
    //formatter... other configuration like rounding mode if that is relevant
    return formatter.string(for: totalSeconds)!
}

Even better is to declare the formatter as a static property outside the function so it doesn't need to be created for each call.

huangapple
  • 本文由 发表于 2023年7月3日 23:06:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76605987.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定