Navigation Menu

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

[SR-13123] Double(_:String) should support strings with grouping characters #55569

Open
swift-ci opened this issue Jun 30, 2020 · 4 comments
Open
Labels
improvement standard library Area: Standard library umbrella

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-13123
Radar None
Original Reporter Rick (JIRA User)
Type Improvement
Additional Detail from JIRA
Votes 0
Component/s Standard Library
Labels Improvement
Assignee None
Priority Medium

md5: 8ba1ca8a87224f28b60a97f254045948

Issue Description:

In English, a number like "10,204" (including the comma [acting as a grouping character]) is a valid string. However,

Double("10,204") produces nil, while Double("10204") does not.

The Standard Library should be updated such that both inputs produce the same (correct) output.

@CodaFi
Copy link
Member

CodaFi commented Jul 2, 2020

@tbkka Thoughts?

@tbkka
Copy link
Contributor

tbkka commented Jul 3, 2020

I'm reluctant to add this to the default (non-locale-aware) initializer.

Two concerns:

  1. I want to leave the default initializer here very plain with no options so we can drive performance for things like JSON parsing.
  2. As Richard says, this is true "in English" but other languages handle things differently.

I would prefer a separate initializer such as `Double("10,204", locale: ....)` or `Double("10,204", options: ...)` that can handle this in varying ways depending on needs. There are likely other designs, of course; I don't think we yet have an established pattern for how to handle locale-sensitive issues in the standard library.

@stephentyrone Do you have a better idea?

@swift-ci
Copy link
Collaborator Author

swift-ci commented Jul 3, 2020

Comment by Richard Aurbach (JIRA)

Actually, I agree with Tim Kientzie. I've been considering an extension of Double that does this. Should be quite simple to do. Actually, let me restate my original post:

There should be a standard way to convert user-entered numeric strings (of all relevant types) to handle common user input strings, such as numbers which contain group separators. And this should be done in a locale-aware way.

I'm fine with XXX(_:String, locale: Locale) where XXX is Int, Double, Float, etc. I just think that since converting user input is a common use of these initializers, that it would be a good addition to Standard Library to include them.

@stephentyrone
Copy link
Member

Locale-dependent formatting is something that we've fairly intentionally wanted to keep out of the standard library (and leave to higher-level libraries).

It's an important feature, but not all important features need to live in the standard library proper. Foundation already provides this functionality (and much more), FWIW: https://developer.apple.com/documentation/foundation/numberformatter

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
improvement standard library Area: Standard library umbrella
Projects
None yet
Development

No branches or pull requests

4 participants