Ferial Adrian
Ferial Adrian

Reputation: 83

Convert String to Float with Large Number

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

Upvotes: 1

Views: 1726

Answers (2)

Chrisvin Jem
Chrisvin Jem

Reputation: 4060

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));
}

Upvotes: 2

Michael Wasser
Michael Wasser

Reputation: 1

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

Upvotes: 0

Related Questions