I'm trying to get conversion fro user entered
money strings to NSDecimalNumber
var nsdecimalNumberFromUserInput: NSDecimalNumber? {
let parsed = NSDecimalNumber(string: self, locale: Locale.current)
if parsed == .notANumber {
return nil
}
return parsed
}
UIKeyboardType == .numberPad in UITextField
somehow displays either . or , depending on
the device and ios version (12.4[.1] & 13 beta 8).
All current locales set to Belarus.
What's a reliable way to parse money regardless
of the curved balls .numberPad sends my way?
The decimal separator in the above mentioned locale is , (comma)
Thanks
Related
When using the Speech framework, I am consistently noticing zero confidence values for certain locales (e.g. "vi-VN", "pt-PT", ...), while non-zero, accurate confidence values are returned for other locales (e.g. "ko-KR", "ja-JP", ...).
Looking at the documentation, the confidence would be zero if there was no recognition. However, when the zero confidence occurs, the formattedString of the bestTranscription is populated and accurate (same for each segment substring text).
I have tried instantiating the locales in various ways (language code only, language and region code, -/_ formatting, grabbing an instance directly off of the SFSpeechRecognizer.supportedLocales() array). I have also tried setting the defaultTaskHint of SFSpeechRecognizer and taskHint of SFSpeechRecognitionRequest to dictation.
I am stuck at this point. Any help would be appreciated. Thanks in advance :)
guard let locale = Locale(identifier: "vi-VN"),
let recognizer = SFSpeechRecognizer(locale: locale),
recognizer.isAvailable else {
return
}
recognizer.defaultTaskHint = .dictation
let request = SFSpeechURLRecognitionRequest(url: ...)
request.contextualStrings = ...
request.shouldReportPartialResults = true
request.taskHint = .dictation
recognizer.recognitionTask(with: request) { (result, error) in
...
if (result.isFinal) {
let transcription = result.bestTranscription
/// transcription.formattedString is correct
/// all segments confidence values are 0, but with the properly recognized substring text.
}
...
}
Is there any way of printing numbers into proper spellings instead of throwing numbers while recording voice via SFSpeechRecognizer? I've tried to get the word format by implementing the code below:
if let resultString = result?.bestTranscription.formattedString {
if let number = Double(resultString) {
let numberFormatter = NumberFormatter()
numberFormatter.numberStyle = .spellOut
let numberString = numberFormatter.string(from: NSNumber(value: number))
let numberStringWithoutHyphen = numberString?.replacingOccurrences(of: "-", with: " ")
print(numberStringWithoutHyphen)
}
}
This solution works great if the user is speaking whole numbers or even decimal numbers but there are some cases where this solution doesn't work at all and makes this solution look dumb. For example, if the user says "Fifty five point zero", the speech recognizer picks it up as "55.0". But the number formatter returns "Fifty five". In an extreme case, if the user says "One two three four", the speech recognizer picks it up as "1234" but the number formatter returns "One thousand two hundred thirty four".
What I am aiming for is if the user says any number, the speech recognizer should return the same, word by word. If the user says "Fifty five point zero", it should return "Fifty five point zero". If the user says "One two three four", it should return "One two three four".
I am trying to get some decimal number from the user inside a UITextfield in iOS Swift. Now the user can input number in his or her local number format as per the locale Settings in iOS. I want to convert this number which is in the user's mother tongue into English number. I searched a lot in this site (stackoverflow.com) and the majority of answers are for conversion from one locale (Chinese, or Arabic or Persian) into English but I want to convert number inputted into any locale format into English. How can I do this? So in nutshell, my question is whether the number being inputted in UITextField is in Hindi, Arabic, Persian, Chinese or whatsoever format as per the locale, I want to convert it into English Number format.
you can use NumberFormatter for that.
check below example:
let numberFormatter = NumberFormatter()
numberFormatter.numberStyle = .decimal
let localNumberInStr = "૨૩"
guard let str = numberFormatter.number(from: localNumberInStr) else {return}
print(str) //"23"
When you check the devices locale you know which locale the user is using.
let locale = Locale.current
Just to improve upon Dharmesh answer, here is the answer wrapped in a helper method for use throughout the code. Obviously, it assumes that while getting user input via UITextField one has considered the number set in the user's locale settings.
func convertLocaleNumberIntoEnglish(localeNumberString: String?) -> String? {
guard let ulocaleNumberString = localeNumberString else {return nil}
let numberFormatter = NumberFormatter()
numberFormatter.numberStyle = .decimal
let localNumberInStr = ulocaleNumberString
guard let number = numberFormatter.number(from: localNumberInStr) else {return nil}
let str = String(format:"%f", number.doubleValue)
return str
}
I am trying to find out which decimal separator is used by the decimal pad keyboard in iOS, so I can convert strings entered by the user to numbers with NumberFormatter and back.
As I want to pre-fill the text field with an existing value, I need to have a number formatter that uses the same decimal separator as the decimal pad keyboard.
The language that my device is set to (German, Germany) uses a comma as the decimal separator. I have configured iOS to have the German keyboard as the primary and active keyboard and English (US, QWERTY) as a secondary keyboard.
The app that I am working on only has a base localization, which is English. In the scheme settings, region and language are set to system default.
If I run my app, the decimal separator used by the decimal pad keyboard is ".", which is the decimal separator used by the en-US keyboard, but not the de-DE keyboard. The normal alphabetic keyboard shows the German keyboard layout.
If I remove the en-US keyboard on the iOS device, the decimal separator changes to ",".
How can I reliably find out, which decimal separator is used by the decimal pad keyboard?
None of the solutions that I have tried so far work:
Using the preset decimalSeparator of NumberFormatter always gives ",".
Using Locale.current.decimalSeparator always returns "," as well.
Using textField.textInputMode?.primaryLanguage to figure out the locale always returns de-DE.
Using Bundle.main.preferredLocalizations to figure out the localization used by the app always returns en.
This is how the number formatter is configured:
let numberFormatter = NumberFormatter()
numberFormatter.minimumIntegerDigits = 1
numberFormatter.minimumFractionDigits = 0
numberFormatter.maximumFractionDigits = 2
Edit: It seems to be possible to determine the locale used by the decimal pad by finding matches between the active text input modes and app localizations:
let inputLocales = UITextInputMode.activeInputModes.compactMap {$0.primaryLanguage}.map(Locale.init(identifier:))
let localizations = Bundle.main.preferredLocalizations.map(Locale.init(identifier:))
let locale = inputLocales.flatMap { l in localizations.map {(l, $0)}}
.filter { preferredLanguage, preferredLocalization in
if preferredLocalization.regionCode == nil || preferredLanguage.regionCode == nil {
return preferredLanguage.languageCode == preferredLocalization.languageCode
} else {
return preferredLanguage == preferredLocalization
}
}
.first?.0
?? Locale.current
numberFormatter.locale = locale
However this solution has several disadvantages:
I do not know whether UIKit selects the decimal separator exactly this way. The behavior may be different for some languages
It has to be computed every time a number will be formatted, as the user may change keyboards while the app is running.
My experience is that when you only support English localization, the decimal separator in the decimal keyboard type, will always be .. So you need to force en_US locale in the NumberFormatter when parsing a number from a string.
Here is a code snippet which tries to parse first using en_US, then tries to parse using Locale.current.
func parseNumber(_ text:String) -> Double? {
// since we only support english localization, keyboard always show '.' as decimal separator,
// hence we need to force en_US locale
let fmtUS = NumberFormatter()
fmtUS.locale = Locale(identifier: "en_US")
if let number = fmtUS.number(from: text)?.doubleValue {
print("parsed using \(fmtUS.locale)")
return number
}
let fmtCurrent = NumberFormatter()
fmtCurrent.locale = Locale.current
if let number = fmtCurrent.number(from: text)?.doubleValue {
print("parsed using \(fmtCurrent.locale)")
return number
}
print("can't parse number")
return nil
}
I would appreciate if Apple would add the Locale.current decimal separator as the decimal separator for decimal keyboard types, or else we need to add localization for all locales in order to get this right.
I'm facing a strange scenario in my swift code. I have multiple uitexfields that I have set their keyboard type to decimalPad. My users will be Arabic, so they will either input numbers in English or Arabic. I've no problem with English integers and decimals and Arabic integers. However, if they try to enter arabic decimal numbers, the app crashes. The decimalPad in the iphone shows "," as a decimal separator for Arabic numbers, so I tried to change it to "." because I had to convert Arabic numbers to English ones before doing any mathmatical operation.
In the xCode simulator, it changes "،" to "." and then i can add or subtract or whatever i want with the user's input. BUT i can't do that if i use "," it just
crashes!!!
Here's part of my code where I change the Arabic numbers to English ones:
if income.text?.isEmpty ?? true {
//income.text = "this is empty"
// incomeNumber += 0.0
} else {
if let temp = income.text {
let replaced = String(temp.characters.map {
$0 == "،" ? "." : $0
})
let incomeStr: String = replaced
let incomeFormatter: NumberFormatter = NumberFormatter()
incomeFormatter.locale = NSLocale(localeIdentifier: "EN") as Locale!
let incomeFinal = incomeFormatter.number(from: incomeStr)
benifit.append(Double(incomeFinal!))
}
}
This code works in the simulator, but if I change "،" to "," it doesn't work even though the iphone numberPad has "," NOT "،"!!!