I need a way to store large numbers with precision. Let's say I want to have 1'234'567'890.123456 in a variable. I was thinking about using Strings ( though it's not recommended ) but I believe there must be a much better way to do this.
BigDecimal gives you numbers with arbitrary precision. It is serialisable so you could save it like that, or use toString() and save that representation.
Take a look at BigDecimal. They are Immutable, arbitrary-precision signed decimal numbers.
Related
How can i check if a number is between two other numbers in Android
if(number1.matches("[1024-65535]+"));
else{
One way to solve your problem is to convert all values that need to be compared into ints. Then the comparison is trivial.
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.
I want to save a lot of strings with SharedPreferences class .
These strings are quit long.
I really want to know the maximum length of a string that can be save in shared preferences in android.And Also How much size of data i can store in This SharedPrefernces class.
As per android architecture there is no such limit to store data in SharedPreference. Better way is to database (SQLite) when you have to deal with huge amount of data
I read somewhere that there is no hard limit other than Integer.MAX_VALUE ( maximal string length). But it is not advisable to store that much on shared preferences, as this is XML file which must be parsed and you will have a problems while parsing it.
I used to store about 50-100KBytes there. It worked.
The exact answer obtained manually is: maximum Unicode symbol's size is 5657632 symbols (or from [0 to 5657631]) in my case. It's some about 2.7MB for SharedPReference.Editor .
Rather large storage.
You ca use this size twice:
PreferenceManager.getDefaultSharedPreferences(c)
context.getSharedPreferences("<key>", <Mode>);
Of course is't limit for SharedPreferences but if system won't have enough memory it is one first stuff what DELETE it, you remember it.
To store an integer in an Android preference, I would intuitively go for EditTextPreference and do the usual String-int-String conversions.
But then I came across a piece of code that stores an integer in a <ListPreference> instead:
<ListPreference
android:key="#string/total_score"
android:defaultValue="0" />
and retrieves it using preferences.getInt(getString(R.string.total_score), 0);
Does this really work? If so, how?
Is it considered acceptable practice?
UPDATE: Thanks to the answers below, I have been able to find the implementation source code for getInt(). I am posting it here for easy reference:
jint android::content::SharedPreferences::getInt(local_ref< java::lang::String > const &a0, jint a1)
{
return call_method<
android::content::SharedPreferences::J2CPP_CLASS_NAME,
android::content::SharedPreferences::J2CPP_METHOD_NAME(2),
android::content::SharedPreferences::J2CPP_METHOD_SIGNATURE(2),
jint
>(get_jobject(), a0, a1);
}
In theory, yes you can store an integer with a ListPreference. After all it's a UI-preference that maps a user displayed label/key (android:entries) to an internal value (android:entryValues) and displays all those mapping options in a listview. That internal value might as well be an integer. You could use a <integer-array>-resource for the entryValues.
In practice, I've never seen that work - it's bugged.
Of course, you can set a int value to the preference key of the ListPreference in your code, since it's a normal preference internally. But that would defeat the whole purpose of predefined resource arrays and ability to select from a list. As a workaround, if a int-array would be handy, I recommend using a <string-array> for the values and convert them an integer in code, as you would with your EditTextPreference
Check this question for a non-working example. ;)
To answer your title question which one to choose for an integer: Depends.
You can use either one with the workarounds.
If the user should be able to enter any value (or just a lot of values), the EditTextPreference is the way to go. A ListPreference would just be too long.
If you have a small set of predefined ints, use a ListPreference. Thats way more comfortable to use and might be displayed with useful labels. Example: If the user is supposed to select a timing interval, you could map the seconds in the value and display a different label, e.g. an hour [value 3600; label "Hour"].
Edit: Also got an idea where your code snippet may be related to. Since this ListPreference does neither specify android:entries nor android:entryValues, it might just be part of a default preference file. You can use PreferenceManager.setDefaultValues() with an XML file to reset/initalize all your preference keys. In this case it's completely random which preference you choose, because all fields that count are android:key and android:defaultValue. You might use any other type in this case, does not matter.
I think what's going on is that the ListPreference is storing the entry values as an array of chars, or in other words an array of bytes (I'm inferring this from the setEntryValues(CharSequence[] entryValues) method). This could be why storing a number works, because the value is stored into the byte array, so using the preferences.getInt(...) method will still work. This could be dangerous, however, as discrepancies with signing could occur, so I wouldn't recommend it, and would parse the int from a string preference instead.
I want to know how to add two 40 digit numbers both in Objective C and Android.
Example:
4000000000000000000000000000000000000000000000000000000000
+5000000000000000000000000000000000000000000000000000000000
How to store and where to store these values?
What is the solution for this?
In Android you can use the BigInteger class for arbitrary sized integers. As for storing them, depends on what you need to store them for, but android has a number of storage options described here that should be able to store these large integers.
in case of objective-c, following code is been used to do the calculation
NSDecimalNumber *aNumber = [NSDecimalNumber decimalNumberWithString:#"4000000000000000000000000000000000000000"];
NSDecimalNumber *bNumber = [NSDecimalNumber decimalNumberWithString:#"2000000000000000000000000000000000000000"];
NSDecimalNumber *cNumber = [aNumber decimalNumberByAdding:bNumber];
NSLog(#"%#", cNumber);
here we use the NSDecimalNumber.