19

When I call this method, an error received. How to handle NaN or infinite value during assign to hour, min or sec?

Here is my code:

private func secondsToFormattedString(totalSeconds: Float64) -> String{
    let hours:Int = Int(totalSeconds.truncatingRemainder(dividingBy: 86400) / 3600)

    let minutes:Int = Int(totalSeconds.truncatingRemainder(dividingBy: 3600) / 60)
    let seconds:Int = Int(totalSeconds.truncatingRemainder(dividingBy: 60))

    if hours > 0 {
        return String(format: "%i:%02i:%02i", hours, minutes, seconds)
    } else {
        return String(format: "%02i:%02i", minutes, seconds)
    }
}
Hamish
  • 78,605
  • 19
  • 187
  • 280
Shahbaz Akram
  • 1,598
  • 3
  • 28
  • 45
  • 1
    Consider to use `TimeInterval` and `DateComponentsFormatter` – vadian Feb 02 '17 at 10:52
  • 2
    How do you call the function and what is the value of totalSeconds when the problem occurs? – A self-contained, reproducible test case would be helpful. – Martin R Feb 02 '17 at 10:53

2 Answers2

50

You should check if totalSeconds is a valid value, like:

guard !(totalsSeconds.isNaN || totalSeconds.isInfinite) else {
    return "illegal value" // or do some error handling
}

And check this: Convert Float to Int in Swift

Community
  • 1
  • 1
Andreas Oetjen
  • 9,889
  • 1
  • 24
  • 34
  • 13
    Neither a nan or infinity is finite, so you could just say `guard totalSeconds.isFinite else {...}`. – Hamish Feb 02 '17 at 11:34
  • @Hamish You're definitly correct. I just wanted to point out the different APIs, because the original Question explicitly mentioned NaN and Infinity. – Andreas Oetjen Feb 02 '17 at 13:31
1

Another option is to use a formatter for this, I just wanted to convert a Double to an Int to make it easier to display in my UI so a NumberFormatter was perfect, my Double was NaN so that's what the NumberFormatter provided me, rather than the fatalError that the Int 'cast' provided (why doesn't it return an optional like Int(String) does?)

But in the context of this question a DateFormatter is a great solution, which would do the whole function's work for you (bear in mind that creating DateFormatters is a little costly so you wouldn't want to create one for every string formatting you do but keep it hanging out if it makes sense)

CMash
  • 1,987
  • 22
  • 35