Type conversion for multilanguage java - android

My app supports multi language, i have a problem when dealing with numericals. Suppose my selected language is hindi and I perform some calculation operation on the value taken from edit text. I had to format the float value after the calculation (say like my resulting calculation gives me 12.345678) i will use the decimal format class to get only 12.34 (like only two digits after decimal point). Till here is fine, but i wanted the resulting string from decimalformat class( i.e: 12.34) to be back as a float. Here the crash happens stating NumberFormatException. Because the resulting 12.34 comes out as a hindi 12.34
How can i format the resulting calcution (i.e: 12.3456) to 12.34 without converting it to string(i.e: decimalFormat.format(value)).

Related

How to format a number as currency using only currency code?

My business problem is as follows: I have a currency code (e.g. "USD") and a number (either float or integer, I can parse either way) and I need to format this number to a currency string using the currency code. For example, 124.3222 and "USD" should create the string "$124.32".
I can create a Currency instance using Currency.getInstance(String), which gives me the symbol and some other information. However, it does not provide any way to format the number as a string. On the other side of the problem, NumberFormat contains several static methods that return a NumberFormat instance capable of doing what I need (e.g. NumberFormat.getCurrencyInstance()).
The problem with NumberFormat is they are all centered around either the default locale, or a passed-in Locale. Given the nature of this app, locale is meaningless. There is no correlation between locale and the currency I need to format as a string. I can use neither the default locale, nor do I have any sort of locale identifier ISO value. All I have is the currency code.
It seems like I'm so close yet so far. There is (in my opinion) an odd disconnect between Currency and the NumberFormat.getCurrencyInstance. Currency can parse a currency code but not locale and cannot format strings, while Locale cannot parse a currency code but can format strings. Am I missing something here?
Edit: I should clarify that I can manually format the number using the symbol and decimal count provided by Currency instance, but I don't see how to figure out where to put the symbol in the string. At any rate, it seems like I should use the built-in currency formatting whenever possible.
I discovered there is a setCurrency() method on NumberFormat. So what I am doing is calling NumberFormat.getCurrencyInstance() so I get a formatter for my locale, and then calling setCurrency() on it for the currency I need to format. This seems to do what I need.

Error using native smiles of Android in text field (Unity3d)

I make application with Unity3d and build it for Android, when I write in input field android native smiles - I got error in line
(invalid utf-16 sequence at 1411555520 (missing surrogate tail)):
r.font.RequestCharactersInTexture(chars, size, style);
chars contains string than contains native android smiles. How I may support native smiles? I use own class for Input Field.
Unfortunately, supporting emojis with Unity is hard. When I implemented this feature, it took about a month to finish it, with a custom text layout engine and string class. So, if this requirement is not particularly important, I would suggest axing this feature.
The reason behind this particular error is that Unity gets characters from the input string one by one, and updates the visual string every character. From the layman point of view, this makes complete sense. However, it doesn't take into account how UTF-16 encoding, which is used in C#, works.
UTF-16 encoding uses 16 bits per a single unicode characters. It is enough for almost all characters that you would normally use. (And, as every developer knows, "almost all" is a red flag that will lay dormant for a long time and then will explode and destroy everything you love.) But it so happens, that Emoji characters are do not fit into 16 bit UTF-16 character, and use a special case — surrogate pair:
Surrogate pair is a pair of UTF-16 characters that represent a single Unicode character. That means that they don't have any meaning on their own individually, and when you try to render a UTF-16 character that is a surrogate head or surrogate tail, you can expect to get an error like this, or something similar.
Essentially, what you need to implement is some kind of buffer, that will accept C# UTF-16 characters one by one, and then pass them to rendering code when it verifies that all surrogate pairs are closed.
Oh, and I almost forgot! Some Emoji characters, like country flags, are represented by two unicode characters. Which means that they can potentially take up to four UTF-16 characters. Aren't text encodings fun?

format edittext input price format

When we work with the currency we need to use ',' separator in appropriate places . For example 1500 as 1,500.
I have a issue. My applications require formatting the EditText's value while typing. I.E., a number that needs to be formatted with decimal and thousands separators. Example, I input 123456789, the EditText display 123.456.789.
How to I do this issue ? Thank all.

convert a double scientific notation to decimal notation in JNI

I have jdouble(double) value coming in from my activity which when printed gives in scientific notation i.e in E format. Now i want to show it in decimal format and truncate it so i used "%.2f" in my format specifier to do that. But weirdly the "%f" format specifier is showing 0.000000 as the final value. please suggest some advice.
P.S I'm doing this on the native side of android.
In order to show decimals instead of E format notation and truncate a number you can use NumberFormat:
NumberFormat formatter = new DecimalFormat("#0.000");
An then, where you want to use your number:
formatter.format(yournumber)
e.g.:
Toast.makeText(context, "Truncated number: " + formatter.format(yournumber), Toast.LENGTH_LONG).show();
In this case, your number will be shown with 3 decimals.
Add as many zeros as decimals you want to show.

What are my options for displaying characters that Android can't?

I discovered today that Android can't display a small handful of Japanese characters that I'm using in my Japanese-English dictionary app.
The problem comes when I attempt to display the character via TextView.setText(). All of the characters below show up as blank when I attempt to display them in a TextView. It doesn't appear to be an issue with encoding, though - I'm storing the characters in a SQLite database and have verified that Android can understand the characters. Casting the characters to (int) retrieves proper Unicode decimal escapes for all but one of the characters:
String component = cursor.getString(cursor.getColumnIndex("component"));
Log.i("CursorAdapterGridComponents", "Character Code: " + (int) component.charAt(0) + "(" + component + ")");
I had to use Character.codePointAt() to get the decimal escape for the one problematic character:
int codePoint = Character.codePointAt(component, 0);
I don't think I'm doing anything wrong, and as String's are by default UTF-16 encoded, there should be nothing preventing them from displaying the characters.
Below are all of the decimal escapes for the seven problematic characters:
⺅ Character Code: 11909(⺅)
⺌ Character Code: 11916(⺌)
⺾ Character Code: 11966(⺾)
⻏ Character Code: 11983(⻏)
⻖ Character Code: 11990(⻖)
⺹ Character Code: 11961(⺹)
𠆢 Character Code: 131490(𠆢)
Plugging the first six values into http://unicode-table.com/en/ revealed their corresponding Unicode numbers, so I have no doubt that they're valid UTF-8 characters.
The seventh character could only be retrieved from a table of UTF-16 characters: http://www.fileformat.info/info/unicode/char/201a2/browsertest.htm. I could not use its 5-character Unicode number in setText() (as in "\u201a2") because, as I discovered earlier today, Android has no support for Unicode strings past 0xFFFF. As a result, the string was evaluated as "\u201a" + "2". That still doesn't explain why the first six characters won't show up.
What are my options at this point? My first instinct is to just make graphics out of the problematic characters, but Android's highly variable DPI environment makes this a challenging proposition. Is using another font in my app an option? Aside from that, I really have no idea how to proceed.
Is using another font in my app an option?
Sure. Find a font that you are licensed to distribute with your app and has these characters. Package the font in your assets/ directory. Create a Typeface object for that font face. Apply that font to necessary widgets using setTypeface() on TextView.
Here is a sample application demonstrating applying a custom font to a TextView.

Categories

Resources