Rajesh Panchal
Rajesh Panchal

Reputation: 1170

How to get float value from json?

i am retrieving value from jsondata

HttpClient client = new DefaultHttpClient();
HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000);
HttpResponse response;
HttpGet gett = new HttpGet(URL);

response = client.execute(get);
BufferedReader reader = new BufferedReader(new putStreamReader(response.getEntity().getContent(), "UTF-8"));
orignalResponse = reader.readLine();

JSONTokener tokener = new JSONTokener(orignalResponse);
JSONObject jsonData = new JSONObject(tokener);
Log.v("AMOUNT", "amount :"+jsonData.get("Amount"));

I want Retrieve "Amount" which originally is "23868352383.00"
but while i am retrieving it using

jsonData.get("Amount"), it gives value as "2.3868352383E10",

using jsonData.getDouble("Amount"), it gives value as "2.3868352383E10"

using jsonData.getLong("Amount") it removes fraction part

How can i retrieve the value ?? please help.

Upvotes: 3

Views: 28997

Answers (5)

jenos kon
jenos kon

Reputation: 564

how to get float values like that 1.5 from json my float number from json look like that :"rating" : 4.5,

and my code in android studio i get this float value by writing:

float ratingNumber = BigDecimal.valueOf (hit.getDouble("rating")).floatValue();

it is work fine for me

Upvotes: 0

Pradeep Kumar Kushwaha
Pradeep Kumar Kushwaha

Reputation: 2239

Simply use this:

float value = Float.valueOf(jsonObject.getString("KEY_STRING"));

Upvotes: 4

hata
hata

Reputation: 12478

The BigDecimal class has a method floatValue. So, If you want to get rather float than String that you can also use it.

BigDecimal.valueOf(jsonObject.getDouble("KEY_STRING")).floatValue();

Upvotes: 13

Syeda Zunaira
Syeda Zunaira

Reputation: 5207

You can try Big Decimal to convert your string to float removing exponents

check it

           BigDecimal.valueOf(yourvalueString);

Hope it Help.

You can get the further help from here Thanks

Upvotes: 4

Sherif El Nady
Sherif El Nady

Reputation: 527

BigDecimal.valueOf(jsonData.getDouble("Amount")).toPlainString()

Upvotes: 5

Related Questions