Open
Description
Description
NumberFormatter not respecting decimal numberStyle.
Steps to reproduce
import UIKit
extension String {
func toDecimalWithAutoLocale() -> Decimal? {
let formatter = NumberFormatter()
formatter.numberStyle = .decimal
// Explicitly tell the formatter to generate decimal numbers
formatter.generatesDecimalNumbers = true
formatter.locale = Locale(identifier: "en_US")
if let number = formatter.number(from: self) as? NSDecimalNumber {
return number as Decimal
}
// If none work, return nil
return nil
}
}
if let decimalValue = "9.20".toDecimalWithAutoLocale() {
print("Decimal value:", decimalValue) // Should print: Decimal value: 9.20
} else {
print("Could not parse decimal value.")
}
Output = Decimal value: 9.199999999999999
Expected behavior
The output should be "9.20"
Environment
- Swift compiler version info
swift-driver version: 1.87.1 Apple Swift version 5.9 (swiftlang-5.9.0.128.108 clang-1500.0.40.1) Target: arm64-apple-macosx14.0
- Xcode version info
Xcode 15.0 Build version 15A240d
- Deployment target: Playground
Metadata
Metadata
Assignees
Labels
No labels