(NumberFormatter.RoundingMode)NSNumberFormatterRoundingMode详解

2017-01-14 10:02:42来源:http://www.jianshu.com/p/2b4a5b91aa16作者:xiAo__Ju人点击

``enum RoundingMode : UInt {        case ceiling        case floor        case down        case up        case halfEven        case halfDown        case halfUp    }``

ceiling

ceiling.png
``        let formatter = NumberFormatter()        formatter.roundingMode = .ceiling        formatter.string(from: -1.1) // -1        formatter.string(from: 1.1) // 2``

floor

floor.png
``         let formatter = NumberFormatter()        formatter.roundingMode = .floor        formatter.string(from: -1.1) // -2        formatter.string(from: 1.1) // 1``

down

down.png
``        let formatter = NumberFormatter()        formatter.roundingMode = .down        formatter.string(from: 1.99) // 1        formatter.string(from: -1.1) // -1``

up

up.png
``        let formatter = NumberFormatter()        formatter.roundingMode = .up        formatter.string(from: 0.01) // 1        formatter.string(from: -1.1) // -2``

halfEven

halfEven.png
``        let formatter = NumberFormatter()        formatter.roundingMode = .halfEven        formatter.string(from: 0.5) // 0        formatter.string(from: 1.5) // 2        formatter.string(from: -0.5) // -0        formatter.string(from: -1.5) // -2``

halfDown

halfDown.png
``        let formatter = NumberFormatter()        formatter.roundingMode = .halfDown        formatter.string(from: 0.5) // 0        formatter.string(from: 0.51) // 1        formatter.string(from: 1.5) // 1        formatter.string(from: 1.51) // 2        formatter.string(from: -0.5) // -0        formatter.string(from: -0.51) // -1        formatter.string(from: -1.5) // -1        formatter.string(from: -1.51) // -2``

halfUp

halfUp.png
``        let formatter = NumberFormatter()        formatter.roundingMode = .halfDown        formatter.string(from: 0.5) // 1        formatter.string(from: 0.51) // 1        formatter.string(from: 1.5) // 2        formatter.string(from: 1.51) // 2        formatter.string(from: -0.5) // -1        formatter.string(from: -0.51) // -1        formatter.string(from: -1.5) // -2        formatter.string(from: -1.51) // -2``