netatmoBench
netatmoBench

Reputation: 85

How to change from negative number to zero in Swift

I have Double data type, cause I need result with floating number, but if my result is negative, it broke all my algorithm. Is there maybe a unsigned data type with floating point?

enter image description here

Upvotes: 5

Views: 5051

Answers (1)

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726987

Use max to limit numbers from going below zero:

let posOrZero = max(possiblyNegative, 0)

If possiblyNegative is above zero, it's going to be the result of max; Otherwise, zero is returned.

Upvotes: 43

Related Questions