Я обнаружил странное поведение в NumberFormatter Swift.
func testNumberFormat() {
let numberFormat = { (number: NSNumber) -> (String?) in
let formatter: NumberFormatter = NumberFormatter()
formatter.numberStyle = .decimal
formatter.maximumFractionDigits = 2
formatter.minimumFractionDigits = 2
formatter.roundingMode = .halfUp
return formatter.string(from: number)
}
XCTAssertEqual(numberFormat(Decimal(0.004) as NSNumber)!, "0.00") // OK
XCTAssertEqual(numberFormat(Decimal(0.005) as NSNumber)!, "0.01") // OK
XCTAssertEqual(numberFormat(Decimal(10000.004) as NSNumber)!, "10,000.00") // OK
XCTAssertEqual(numberFormat(Decimal(10000.005) as NSNumber)!, "10,000.01") // NG!
XCTAssertEqual(numberFormat(Decimal(10000.014) as NSNumber)!, "10,000.01") // OK
XCTAssertEqual(numberFormat(Decimal(10000.015) as NSNumber)!, "10,000.02") // OK
}
Следующий тест не пройден.
XCTAssertEqual(numberFormat(Decimal(10000.005) as NSNumber)!, "10,000.01")
Я ожидаю "10 000, 01", но "10 000, 00"был возвращен.Мое понимание ошибочно?