Skip to content

Instantly share code, notes, and snippets.

@withzombies
Last active September 28, 2022 17:40
Show Gist options
  • Save withzombies/e718a1b80f7e93f48c1614739a559a46 to your computer and use it in GitHub Desktop.
Save withzombies/e718a1b80f7e93f48c1614739a559a46 to your computer and use it in GitHub Desktop.
Parsing a locale decimal number
func testFoundationDouble() {
let locales: [Locale] = [Locale(identifier: "en_US"), Locale(identifier: "de_DE"), Locale(identifier: "es_AR"), Locale(identifier: "es_ES"), Locale(identifier: "es_MX"), Locale(identifier: "fr_FR")]
let numbers: [Double] = [0.01, 0.1, 1, 1_100.10, 100_000_000]
for locale in locales {
let nf = NumberFormatter()
nf.locale = locale
nf.groupingSize = 3
nf.usesGroupingSeparator = true
nf.minimumSignificantDigits = 2
print("locale: \(locale.identifier)")
for number in numbers {
print("input number: \(number)")
let ns_number = NSNumber(value: number)
if let str = nf.string(from: ns_number) {
print(" - locale formatted str: \(str)")
if let parsedDouble = nf.number(from: str) {
// parsedDouble and the original input value should be equal
print(" - round trip double: \(parsedDouble)")
XCTAssertEqual(ns_number, parsedDouble, "numbers must be equal. original: \(number), String: \(str) converted: \(parsedDouble), locale: \(locale.identifier)")
}
}
}
print("\n\n")
}
}
locale: en_US
input number: 0.01
- locale formatted str: 0.010
- round trip double: 0.01
input number: 0.1
- locale formatted str: 0.10
- round trip double: 0.1
input number: 1.0
- locale formatted str: 1.0
- round trip double: 1
input number: 1100.1
- locale formatted str: 1,100.1
- round trip double: 1100.1
input number: 100000000.0
- locale formatted str: 100,000,000
- round trip double: 100000000
locale: de_DE
input number: 0.01
- locale formatted str: 0,010
- round trip double: 0.01
input number: 0.1
- locale formatted str: 0,10
- round trip double: 0.1
input number: 1.0
- locale formatted str: 1,0
- round trip double: 1
input number: 1100.1
- locale formatted str: 1.100,1
- round trip double: 1100.1
input number: 100000000.0
- locale formatted str: 100.000.000
- round trip double: 100000000
locale: es_AR
input number: 0.01
- locale formatted str: 0,010
- round trip double: 0.01
input number: 0.1
- locale formatted str: 0,10
- round trip double: 0.1
input number: 1.0
- locale formatted str: 1,0
- round trip double: 1
input number: 1100.1
- locale formatted str: 1.100,1
- round trip double: 1100.1
input number: 100000000.0
- locale formatted str: 100.000.000
- round trip double: 100000000
locale: es_ES
input number: 0.01
- locale formatted str: 0,010
- round trip double: 0.01
input number: 0.1
- locale formatted str: 0,10
- round trip double: 0.1
input number: 1.0
- locale formatted str: 1,0
- round trip double: 1
input number: 1100.1
- locale formatted str: 1100,1
- round trip double: 1100.1
input number: 100000000.0
- locale formatted str: 100.000.000
- round trip double: 100000000
locale: es_MX
input number: 0.01
- locale formatted str: 0.010
- round trip double: 0.01
input number: 0.1
- locale formatted str: 0.10
- round trip double: 0.1
input number: 1.0
- locale formatted str: 1.0
- round trip double: 1
input number: 1100.1
- locale formatted str: 1,100.1
- round trip double: 1100.1
input number: 100000000.0
- locale formatted str: 100,000,000
- round trip double: 100000000
locale: fr_FR
input number: 0.01
- locale formatted str: 0,010
- round trip double: 0.01
input number: 0.1
- locale formatted str: 0,10
- round trip double: 0.1
input number: 1.0
- locale formatted str: 1,0
- round trip double: 1
input number: 1100.1
- locale formatted str: 1 100,1
- round trip double: 1100.1
input number: 100000000.0
- locale formatted str: 100 000 000
- round trip double: 100000000
Test Case '-[test.Tests testFoundationDouble]' passed (0.005 seconds).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment