Thursday, February 21, 2019

Round decimal number Swift

To round a decimal number, let use this function.
  func round(a:Double)->Double{
        let mu = pow(10.0,1.0)
        let r=round(a*mu)/mu
        return r
    }
Now use it like this.
let number=3.34566
let number2=round(number)
 la.text=String(number2)

If you change let mu = pow(10.0,1.0) to let mu = pow(10.0,3.0)
The result number will has 3 characters after comma.


No comments:

Post a Comment