Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NumberFormatter not respecting decimal numberStyle on macOS #4842

Open
MaddyThakker opened this issue Nov 7, 2023 · 0 comments
Open

NumberFormatter not respecting decimal numberStyle on macOS #4842

MaddyThakker opened this issue Nov 7, 2023 · 0 comments

Comments

@MaddyThakker
Copy link

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant