1

Im using MPandroid chart to inflate Pie Chart, with some String JSON return

i tried to cast String value with float.parseFloat("3584907054456.48")

but it had exponent value when i log it, something like this 3584907E12

i need to get float value 3584907054456.48 is it possible ?

List<String> dataStackedSalesVolume1;
List<String> dataStackedSalesVolume2;

float[] firstDataStacked    = new float[counte];
float[] secondDataStacked   = new float[counte];

int counte  = merchantECommerceDataAll.getData().getMerchantECommerceTipekartuList().getMerchantECommerceTipeKartuData().get(1).getDataSalesVolume().size();
dataStackedSalesVolume1 = merchantECommerceDataAll.getData().getMerchantECommerceTipekartuList().getMerchantECommerceTipeKartuData().get(0).getDataSalesVolume();
dataStackedSalesVolume2 = merchantECommerceDataAll.getData().getMerchantECommerceTipekartuList().getMerchantECommerceTipeKartuData().get(1).getDataSalesVolume();

for (int i=0; i< counte; i++) {
        firstDataStacked[i]         = Float.parseFloat(dataStackedSalesVolume1.get(i));
        secondDataStacked[i]        = Float.parseFloat(dataStackedSalesVolume2.get(i));
}

i tried to get the string and put it into new list and then parse that list and put parsed value into float[]

but it the results is rounded, i need to get the full length of data without rounded

2
  • float is a floating-point number type, having fixed precision for mantissa and exponent, not suitable for storing fixed-point numbers. You won't get better precision than this type provides. Commented May 21, 2019 at 8:56
  • simply saying, float type has no enough space for storing mantissa 0.358490705445648 with full precision, thus it is cut to the closest value which "fits" the space---something like 0.3584907--providing such output. Commented May 21, 2019 at 9:06

2 Answers 2

2

Edit - The BigDecimal value can be converted to float value by using the floatValue() method. (Example - float requiredValue = bigDecimalValue.floatValue();)

Do note however that this will result in a drop in precision.

BigDecimal bigDecimalValue = new BigDecimal("3584907054456.48");
System.out.println(bigDecimalValue); //3584907054456.48

float floatValue = bigDecimalValue.floatValue();
System.out.println(floatValue); //3.58490702E12

//Formatted better to show the drop in precision.
System.out.println(String.format("%.2f", floatValue)); //3584907018240.00

Don't use float, use BigDecimal instead.

Do note that you won't be directly able to use operators such as +,-,*,etc. You'll have to use the provided methods, refer to the official documentation or an article such GeeksForGeeks articles to help you get an initial hang of it.

Sample code -

List<String> dataStackedSalesVolume1;
List<String> dataStackedSalesVolume2;

BigDecimal[] firstDataStacked    = new BigDecimal[counte];
BigDecimal[] secondDataStacked   = new BigDecimal[counte];

int counte  = merchantECommerceDataAll.getData().getMerchantECommerceTipekartuList().getMerchantECommerceTipeKartuData().get(1).getDataSalesVolume().size();
dataStackedSalesVolume1 = merchantECommerceDataAll.getData().getMerchantECommerceTipekartuList().getMerchantECommerceTipeKartuData().get(0).getDataSalesVolume();
dataStackedSalesVolume2 = merchantECommerceDataAll.getData().getMerchantECommerceTipekartuList().getMerchantECommerceTipeKartuData().get(1).getDataSalesVolume();

for (int i=0; i< counte; i++) {
        firstDataStacked[i]         = new BigDecimal(dataStackedSalesVolume1.get(i));
        secondDataStacked[i]        = new BigDecimal(dataStackedSalesVolume2.get(i));
}
Sign up to request clarification or add additional context in comments.

7 Comments

hey, thanks for responding, but i do really need float as data type in order to inflate that data into PieChart, can I convert it into float when I parsed it into big decimal ?
BigDecimal implements the java.lang.Number interface, so you can just use floatValue()
That might not necessarily be a good idea though, the conversion to float using floatValue() might result in a loss of precision as mentioned in the docs.
So, for example, System.out.println(new BigDecimal("3584907054456.48").floatValue()); would print 3.58490702E12. If we further format that, System.out.println(String.format("%.2f",new BigDecimal("3584907054456.48").floatValue())); would print 3584907018240.00. As you can see, there is precision loss.
Its same with float, float = 3584907054456.48f; String.format("%.2f", float); output is 3584907018240,00
|
0

You can use something like BigDecimal.valueOf(new Double("3584907054456.48")) from java.math After this you can divide, compare your value and so on

3 Comments

converting first to a double may cause rounding errors as well. There is at most as much precision as in a double. So it somehow defeats the purpose of BigDecimal.
you propose the worst possible approach
@AlexSalauyou you're right, solution isn't really good :( Must be simpler

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.