Precision String Format Specifier In Swift

前端 未结 30 2052
面向向阳花
面向向阳花 2020-11-22 05:58

Below is how I would have previously truncated a float to two decimal places

NSLog(@\" %.02f %.02f %.02f\", r, g, b);

I checked the docs an

30条回答
  •  名媛妹妹
    2020-11-22 06:17

    @infix func ^(left:Double, right: Int) -> NSNumber {
        let nf = NSNumberFormatter()
        nf.maximumSignificantDigits = Int(right)
        return  nf.numberFromString(nf.stringFromNumber(left))
    }
    
    
    let r = 0.52264
    let g = 0.22643
    let b = 0.94837
    
    println("this is a color: \(r^3) \(g^3) \(b^3)")
    
    // this is a color: 0.523 0.226 0.948
    

提交回复
热议问题