How to truncate float to% .2f format when encoding JSON?

Suppose we have some float f = 52.92; . In fact, it will contain a value similar to 52.91999816894531 . But I want to pass it to my web application using json-string truncating these minor digits. How can I do it?

As a result, I need to get this json string:

 {"price": 52.92} 

The code I'm using is:

 float f = 52.92; JSONObject js_price = new JSONObject(); js_price.put("price", f); Log.d("json", js_price.toString()); 

Produces this ugly json:

 {"price": 52.91999816894531} 

Also, I need "price" be a number format in json, not a string.

+6
source share
6 answers

It seems that I can use constrctor JSONObject (String json) , something like this:

 JSONObject js_price = JSONObject(String.format("{\"price\": %.2f}", f); 

and after that I can do any other things related to json with this object ...

+3
source

Try using String.format

Log.d("json", String.format("%.2f", f));

Edit:

Is a float used or can you try a double?

  double d = 52.92; JSONObject js_price = new JSONObject(); js_price.put("price", d); Log.d("json", js_price.toString()); 
+1
source

AFAIK, you cannot do this with JSONObject .

Since there is no such exact floating point value as 52.92 , you really need to use string formatting rules, but then you cannot process the JSON encoder with this value as a number, and not as a quoted string.

You will need:

 String json = String.format("{\"price\", %.2f }", f); 

In any case, when the JSON is read on the client, it will still not be 52.92 , it will be 52.91999816894531 again. Thus, all that you achieve is a small saving in the amount of your JSON.

Another alternative would be to multiply the number by 100, and then send it as an integer. You will still get 52.91999... if you split it on the client side though!

You can also use a subclass of JSONObject and override this method:

 static public java.lang.String numberToString(java.lang.Number number) 
0
source

Give GSON a try ...

 class Serializer { // You can register custom serilaizers to it to suite your needs private static Gson gson = new GsonBuilder().create(); public static final <T> String toJSON(T clazz) { return gson.toJson(clazz); } } 

then use the following code to get json string

 float f = 52.92; Map<String,Object> js_price = new HashMap<String,Object>() js_price.put("price", f); String json = Serializer.toJSON(js_price) 
0
source

I would use this utility class with my own parameters:

 public class BigDecimalUtils { /** * <code>MathContext</code> with precision 10 for coordinates. */ private final static MathContext MC = new MathContext(10); /** Scale for coordinates. */ private final static int SCALE = 6; public static float format(double aDouble) { BigDecimal bdLatitude = new BigDecimal(aDouble, MC).setScale(SCALE, RoundingMode.HALF_UP); return bdLatitude.floatValue(); } public static float format(float aFloat) { BigDecimal bdLatitude = new BigDecimal(aFloat, MC).setScale(SCALE, RoundingMode.HALF_UP); return bdLatitude.floatValue(); } } 

Then just call js_price.put("price", BigDecimalUtils.format(f));

0
source

You can try this method after the deserializable value

 DecimalFormat dec = new DecimalFormat("###.##"); System.out.println(dec.format(value)); 
-2
source

Source: https://habr.com/ru/post/921866/


All Articles