Reputation: 1
Suppose my JSON is like this.
// {
// "count": 32,
// "weight": 1.13,
// "name": "grape",
// "isFruit": true
// "currentPrice" : "30.00"
// }
If I read my JSON like this,
String current = json.getString("currentPrice");
the current variable will have value as "30.00". Is there any way that I can parse this as an Integer? I tried doing Integer.parseInt but It is giving an error like Number format exception for input string "30.00".
I tried removing quotes by applying regex but didn't work.
Upvotes: 0
Views: 267
Reputation: 6693
You want parseFloat()
. 30.00
isn't an integer, even though it's numerically EQUAL to the integer 30
.
If you want it as an integer, you can use Math.floor()
to convert it to one, or you can use parseInt()
to get the integer portion, but if you really want the whole value (if it might not always be whole), parse it as a float.
Upvotes: 0
Reputation: 44
You need to use
parseInt('current')
parseInt(num); // default way (no radix)
parseInt(num, 10); // parseInt with radix (decimal)
parseFloat(num) // floating point
Number(num); // Number constructor
to get current
Upvotes: 1