简体   繁体   中英

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

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

您可以使用来自java.math BigDecimal.valueOf(new Double("3584907054456.48"))这样的东西,然后进行除法,比较值等。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM