LengthFormatter: rounding error

Originator:CJSpradling
Number:rdar://FB8208224 Date Originated:07/28/2020
Status:Open Resolved:
Product: Product Version:
Classification: Reproducible:
 
LengthFormatter.string(fromMeters:) contains a bug when rounding values that are <0.5 short of an even foot.

e.g.

let formatter = LengthFormatter()
formatter.numberFormatter.maximumFractionDigits = 0
formatter.locale = Locale(identifier: "en_US")
let meters = 1.82
let formattedString = formatter.string(from: meters)

// formattedString is "5 ft 12 in"
// Expected value should be "6 ft 0in"

This is because the value of `1.82 meters` converts to ~71.65 inches. 

However, the calculations "feet = floor(value / 12)" and "inches = value % 12" occur _before_ LengthFormatter.numberFormatter performs its rounding operations, and there is no second check on the rounded inch value to see if feet/inches need to be recalculated afterward

Comments


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!