I have an app which read an import in a TextField then process it using
let number = NSNumberFormatter().numberFromString(numberString)
if let number = number {
let floatValue = Float(number)
}
While I'm working with regional formats which use comma as decimal separator (like Italy or Germany) everything works (I can write 17,80 or 17.80 and they work both). When I work with regional formats which use dot as decimal separator (like USA), when I use dot as decimal separator (17.80) OK but if I use comma (17,80) app crash!
How can I solve it?
Would looking at the string and changing it to whichever format you know (e.g. making it always use a decimal separator) won't cause problems be a viable fix here?
Related
In an ionic 3 app I am currently trying to get a floating point number as user input. Since the application is exclusively targeting a german audience I am expecting input with a comma as the decimal separator, e.g. 750,5.
In addition I would like to open the numerical keyboard.
I tried including an input field with type="text". I parsed the resulting input string using a custom parsing method, which respects the different decimal separator.
Since the input field has a two way binding and the application might change the number itself I convert the number to a string using the Angular decimal pipe like so:
numberString = new DecimalPipe('de').transform(value.count, '1.0-2');
That worked well, however the app always opens the full alphanumerical keyboard.
To show the numerical keyboard I used the input field with type="number".
<ion-input type="number" step="any"></ion-input>
Using this, the numerical shows as expected. However the input field now includes thousand separators. For example if I type 7500, the input field displays 7,500 using the English decimal separator.
Now I am not sure where this problem is caused but the app itself seems to be running under an English locale although the device is set to German. I expect the problem to be solved by setting the web view locale to German. How could I achieve this?
I want to use emojis in my iOS and Android app. I checked the list of emojis here and it lists out the hex code for the emojis. When I try to use the hex code such as U+1F600 directly, I don't see the emoji within the app. I found one other way of representing emoji which looks like \uD83D\uDE00. When using this notation, the emoji is seen within the app without any extra code. I think this is a Unicode string for the emoji. I think this is more of a general question that specific to emojis. How can I convert an emoji hex code to the Unicode string as shown above. I didn't find any list where the Unicode for the emojis is listed.
It seems that your question is really one of "how do I display a character, knowing its code point?"
This question turns out to be rather language-dependent! Modern languages have little trouble with this. In Swift, we do this:
$ swift
Welcome to Apple Swift version 3.0.2 (swiftlang-800.0.63 clang-800.0.42.1). Type :help for assistance.
1> "\u{1f600}"
$R0: String = "😀"
In JavaScript, it is the same:
$ node
> "\u{1f600}"
'😀'
In Java, you have to do a little more work. If you want to use the code point directly you can say:
new StringBuilder().appendCodePoint(0x1f600).toString();
The sequence "\uD83D\uDE00" also works in all three languages. This is because those "characters" are actually what Unicode calls surrogates and when they are combined together a certain way they stand for a single character. The details of how this all works can be found on the web in many places (look for UTF-16 encoding). The algorithm is there. In a nutshell you take the code point, subtract 10000 hex, and spread out the 20 bits of that difference like this: 110110xxxxxxxxxx110111xxxxxxxxxx.
But rather than worrying about this translation, you should use the code point directly if your language supports it well. You might also be able to copy-paste the emoji character into a good text editor (make sure the encoding is set to UTF-8). If you need to use the surrogates, your best best is to look up a Unicode chart that shows you something called the "UTF-16 encoding."
In Delphi XE #$1F600 is equivalent to #55357#56832 or D83D DE04 smile.
Within a program, I use it in the following way:
const smilepage : array [1..3] of WideString =(#$1F600,#$1F60A,#$2764);
JavaScript - two way
let hex = "😀".codePointAt(0).toString(16)
let emo = String.fromCodePoint("0x"+hex);
console.log(hex, emo);
I have currency values within a table and for filtering I want to show only the numbers without currency symbols, but, I need the decimal point, here's what i'm using
$price_check = preg_replace('/\D/', '', str_replace(',','',(str_replace('£','',$item[item_price])));
How do I keep the decimal point? is first question
Also, I'm removing £ first (ascii for GBP) and then removing comma (if present) - is there a better way to do this?
Figured it out, the preg_replace anyway
$price_check = preg_replace('/(\.[0-9]+?)0*$/', '$1', str_replace(',','',(str_replace('£','',$item[item_price]))));
I use UIKeyboardTypeDecimalPad for keyboardType. When I test in mac, I see button decimal in (.), but when I test in iPod, decimal button become (,). How can I fix this ?
This is the code I tested:
self.rielPayTextField.keyboardType = UIKeyboardTypeDecimalPad;
I guess it is a setting related to the 'Region Format' specified on your device.
You can double check your settings going in 'Settings', 'General' and finally choosing 'International'
The thread may be a little older, but here's what I experienced:
The "." and "," depend on your regional setting of your iDevice.
I'm in Germany, so the decimal separator is "," and it showed on the numberpad.
When switching the region and language settings (my app is 4-lingual) to english, I had the "." decimal separator.
So your numberpad should be fine, as long as you work with decimals.
Only thing you have to care about is to check the number that was entered by the user. You have to make sure to use nslocalized number so that the input gets properly converted to be handled by your app. A double uses "." as separator, so entering a decimal of 3,4 in German would result in errors as the colon is not recognized as decimal separator.
I hope this helps.
I have set up a nice NSNumberFormatter which does all the things I want, except allowing a number to be preceded by a unary "+". It's not a common usage normally, but I'm entering latitudes and "+12.34" is sometimes used to indicate a northern latitude in the same way "-43.21" indicates southern. Likewise for longitudes and East/West. It may not be used every time but I don't want to make it 'illegal' -- that just annoys people.
I tried [... setLenient:TRUE] but that doesn't change this behavior.
I'm trying to not set up my own format string to avoid impacting localization.
Any suggestions?
Just a guess, but what happens if you use setPositivePrefix:?
[formatter setPositivePrefix:#"+"];
How about putting a wrapper around the function which either strips the + or outputs it (I can't tell which conversion you're doing) as you expect it?