I show values in my app by:
String(format: "%4.0f", CountedValue)
But it shows value like 241
.
How can I round value by following maths mathematical rules?
For example: 240
instead 241
, or 250
instead 245
.
I show values in my app by:
String(format: "%4.0f", CountedValue)
But it shows value like 241
.
How can I round value by following maths mathematical rules?
For example: 240
instead 241
, or 250
instead 245
.
There aren't any methods that do that, but you could implement them.
For example, for 241 or 242, you could get the modulus of the division by 5 and compare by that.
Example: let mod = number % 5
(results in 1
for 241
, 2
for 242
, etc). As 245
is a boundary case which modulus is 0
, we'll add 1
to the number (basically an offset).
let mod = (number + 1) % 5
var result: Int = number - mod
if mod >= 3 { result += 5 }
This way, result
will be 240
for 241
and 242
, and 245
for 243
, 244
and 245
.