Storing two decimal place value android - android

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.

Related

How to store currency values in Realtime Database

I have the following data. To me both are Double type. But Firebase always takes 69.0 as 69. Then I can't declare the property price as Double because I'm getting the exception:
firebase.database.DatabaseException: Failed to convert value of type
java.lang.Long to Double
Is there a way to force a Double type to a specific property?
If you're going to store money values in a database, consider that floating point numbers can be inaccurate, due to the way they're represented according to the IEEE 754 specification. A full discussion of this is too long to put here.
You can work around this by multiplying all your prices values by some value that eliminates the fractional part of the number and only stores integers. With US dollars, for example, there is no smaller unit of currency than 1 cent. Instead of storing floating point value .01 to represent this (possibly losing precision), I could multiply it by 100 and store an integer 1, without losing any precision.
If I eliminate floating point numbers from my prices like this, now I'm only storing integer values. After reading them on the client, I still have to format them as dollars and cents, which means I have to write some extra code to format prices values like "399" into "$3.99", which is not that difficult, and I can do it without losing any precision.
If you're storing currency values other than US dollars, you may have a different scheme, but the idea is the same - convert your fractional prices into integers for storage in the database, then format those values as the user would expect to see them.

How to revert back from currency format, Android?

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.

how to avoid exponents in android eclipse calculation results in a text string?

in android eclipse sometimes a calculation result for both double and float when displayed as a string uses a decimal point (desired) but sometimes using an exponent (bad - confusing to user). anyway to avoid the exponent?
See String.format documentation.
Just set the desired format for your numbers. You probably want String.format("%f",number).

How to add two 40 digit numbers in Objective-C and Android

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.

Float Precision Display (Android)

I am trying to make a program that takes some user input, runs a few calculations and outputs the answer. My problem is that this answer is sometimes many decimal places long which is causing some aesthetic and layout problems. I only need to display 4 decimal places worth of data. Is there anyway to limit the precision of these numbers at output time? (The Numbers are stored in floats and I'm programming for Android.)
You can format a float to 4 decimal places using String.format.
Example:
String result = String.format("%.4f", theNumber);
See also:
How to nicely format floating numbers to String without unnecessary decimal 0?
String.format(format, args)
Format strings in Java

Categories

Resources