I'm developing Android app which uses this method:
public static String currencyFormat(BigDecimal n) {
return NumberFormat.getCurrencyInstance().format(n);
}
which formats number based on Locale currency.
How to revert back from currency format, e.g. $35, to 35? Note that I cannot just remove First character because different locales have different currency name lengths.
You must store the currency unit in a separate field, encapsulated into some higher-order abstraction such as a final value class (with appropriate equals and hashcode defined), eg called CurrencyAmount. [if you do scala, basically you want a case class]. Any other solution will require you to 'reverse engineer' the unit portion from the amount portion and depending on the complexity of your spec of allowable values, it might be reliable only to various degrees. I would just encode the two portions in their own fields and solve this for all cases.
You might try to cut out all non numeric characters from a String with a regex.
Try this.
Related
I know there are many alternatives to reach what I wish, but I wont this solution because it is the most comfortable to me. I wish to use enum that starts with number, like so.
public enum Quality {
1080p,
720p,
BlueRay //this one OK
}
And then use it like so when converting to string:
Quality.1080p.name();
Why it is not possible?
Because the Java language doesn't allow variable names to start with a number- just a letter or underscore. Any character after the first may be a number. The main reason for this is to make parsing easier, and prevent situations where the parser can't tell if a symbol is a number or a variable name.
For example, if numbers were valid at the start of a variable I could do the following:
String 1 = "string";
System.out.println(1);
Does this print 1 or "string"? They avoid the problem by not allowing it. Many (most?) languages have that restriction.
I am making a request to my local server and getting the following numbers in my response:
<PercentSm>2.40</PercentSm>
<FlatSm>0.00</FlatSm>
<MaxSm>99999999.00</MaxSm>
<FlatLgs>0.20</FlatLgs>
when i save them as Double or Float android rounds them down to one decimal place and the MaxSm field is diplayed as 9.9999999E7.
I want to be able to store them with the exact same value and decimal point like i get them. I tries using DecimalFormat but it only supports API 24 and higher.
Is there any other way of doing this that will support older apis as well? for example API 19 and up?
use NumberFormat
this class can format your numbers for you.NumberFormat is the abstract base class for all number formats. This class provides the interface for formatting and parsing numbers. NumberFormat also provides methods for determining which locales have number formats, and what their names are.
digits used, or whether the number format is even decimal.
You can also use,Formatted printing, With the Formatter Class then parse the results with Integer.parseInt() though this is more less efficient.
Every country uses own measurement units, for example people in US measure temperature in F however Canada and most European countries in C. Same is applied for measurement weight, distance and so on.
I define measurement units in my application as
<string-array name="measure_units">
<item>lb</item>
<item>oz</item>
<item>cwt</item>
<item>ft</item>
<item>yd</item>
for default locale and then for France I define something like
<string-array name="measure_units">
<item>kg</item>
<item>ml</item>
<item>tn</item>
<item>dm</item>
<item>m</item>
So far so good, however it is a huge work to define the values for every country considering tons of duplicates. So I am thinking to introduce 2-4 basic measurement systems and specify values only for them, but how to implement it for Android? Is there any measurement systems mapping embedded in the OS? If there is no such functionality how do you deal with the problem?
SO this kind of thing is part of localization of an app. Generally the rule is- use one system internally in your app (generally metric, but anything will work). Convert to local units only right before display or right after data is input, never store data in anything but your internal format.
You might be able to find a library to do this for you, but its not built into Android or Java. Typically though you don't map every language to a length, temperature, etc. You do just what you suggested- build a couple of linked choices, and map Locales to a specific set of choices. If you pick metric as your default, you only need to do overrides for 2 or 3 countries- pretty much everyone uses metric.
I have a suggestion.
First store the values in international units. (Distance in m, Weight in kg, Temperature in C . . . etc). Then use some methods to convert international units to other units. When ever you need non-international units, use those methods to get the converted values. You can use an if statement or a switch to get the values accordingly.
As an example
if(/*international unit ?*/){
Log.D("International unit : ", temperature + "C");
}else{
Log.D("Non - International unit : ", getTemperatureInFahrenheit(temperature) + "F");
}
float getTemperatureInFahrenheit(float celsius){
return ((celsius * 9 / 5.0) + 32)
}
Is there any good simple way to format fields as I want.
I'd like to separate field value from field text. For example, I'd like to group number by 4, so when I set field's value to 123456789 it would show as 1 2345 6789, but when I get value I'll get number without spaces (123456789).
I think, this is the way widgets works on other tools, i.e. .Net VS. I can set there some mapping, formats and so on, so what is shown depends on value and format/mapping.
In android this must be done for passwords fields. It has text as value, but it shows some dots (value is mypassword and field shows **********).
I guess you just want to display the number in nice format, please use the APIs provided by PhoneNumberUtils.
static String formatNumber(String source)
static void formatNumber(Editable text, int defaultFormattingType)
You could set the formatted string to the text view for displaying nicely. But the saved content is still the raw content without any format.
Please refer to the SDK document at http://developer.android.com/reference/android/telephony/PhoneNumberUtils.html
I'm looking for a way to compare 2 strings partial. I need to clear this with an example.
The base string is "equality".
The string I need to check is spelled wrong: "equallaty". I want to conform this is partially correct so the input, even not right in a grammar way, is the same as the base string.
Now I can of course parse the string to an char array. Now I can check every single character, but if I check the first 4 characters they will be right, the rest will be wrong even if there are only 2 mistakes. So the check I want to use is that a minimum of 70 procent of the characters should match.
Is anyone able to help me get on the right track?
Compare the strings with an edit-distance metric like the Levenshtein distance. Such a metric basically counts the number of changes needed to make the strings equal. If the number of changes is small relative to the total size of the string, then you can consider the strings similar.