public class Statics {
@JsonFormat(shape = JsonFormat.Shape.STRING)
private Double sum;
@JsonFormat(shape = JsonFormat.Shape.STRING)
private Double avg;
@JsonFormat(shape = JsonFormat.Shape.STRING)
private Double max ;
@JsonFormat(shape = JsonFormat.Shape.STRING)
private Double min ;
private Long count = Long.valueOf(0);
}
Using this annotation: @JsonFormat(shape = JsonFormat.Shape.STRING)
Solved my problem!
public class Statics {
@JsonFormat(shape = JsonFormat.Shape.STRING)
private Double sum;
@JsonFormat(shape = JsonFormat.Shape.STRING)
private Double avg;
@JsonFormat(shape = JsonFormat.Shape.STRING)
private Double max ;
@JsonFormat(shape = JsonFormat.Shape.STRING)
private Double min ;
private Long count = Long.valueOf(0);
}
Using this annotation: @JsonFormat(shape = JsonFormat.Shape.STRING)
Solved my problem!
Use String instead of Double do your calculations on double the convert value to String.ValueOf(sum) and set value to your statistics POJO.
Convert your POJO like below
public class Statics {
private String sum;
private String avg;
private String max;
private String min;
private Long count = Long.valueOf(0);//getter and setters and constructrures
}
If you are using Jackson you can use @JsonSerialize and ToStringSerializer :
public MyBean {
@JsonSerialize(using = ToStringSerializer.class)
private double myDouble;
//getter and setter
//constructors
}
The code to test it (Jackson version 2.9.8) :
MyBean myBean = new MyBean(20.3);
ObjectMapper objectMapper = new ObjectMapper();
String json = objectMapper.writeValueAsString(myBean);
System.out.println(json);
The output is :
{"myDouble":"20.3"}
You could either use @JsonSerialize with ToStringSerializer:
@Data
public class MyBean {
@JsonSerialize(using = ToStringSerializer.class)
private double myDouble;
}
Or use @JsonFormat with JsonFormat.Shape.STRING:
@Data
public class MyBean {
@JsonFormat(shape = Shape.STRING)
private double myDouble;
}
Both approaches produce the same result.
If you intend to use these annotations in multiple places, you may consider @JacksonAnnotationsInside which allows you to create a custom annotation which contains one or more Jackson annotations:
@Retention(RUNTIME)
@JacksonAnnotationsInside
@JsonSerialize(using = ToStringSerializer.class)
public @interface JsonString {
}
@Data
public class MyBean {
@JsonString
private double myDouble;
}
java - Why double is converted to int in JSON string - Stack Overflow
java - How can I convert each integer/double value to String from JSON data containing nested objects and arrays? - Stack Overflow
Json to Double type JAVA - Stack Overflow
Is that good idea to use monetary value as String in JSON?
Use with AIdouble total = 44;
String total2 = String.valueOf(total);
This will convert double to String
Using Double.toString(), if the number is too small or too large, you will get a scientific notation like this: 3.4875546345347673E-6. There are several ways to have more control of output string format.
Use with AIdouble num = 0.000074635638;
// use Double.toString()
System.out.println(Double.toString(num));
// result: 7.4635638E-5
// use String.format
System.out.println(String.format ("%f", num));
// result: 0.000075
System.out.println(String.format ("%.9f", num));
// result: 0.000074636
// use DecimalFormat
DecimalFormat decimalFormat = new DecimalFormat("#,##0.000000");
String numberAsString = decimalFormat.format(num);
System.out.println(numberAsString);
// result: 0.000075
Use String.format() will be the best convenient way.
Its in real not getting converted into int. Only thing happening is as JS Object its not showing .0 which is not relevant.
In your sample program, change some of the value from double[] d = new double[]{1.0,2.0,3.0} to
double[] d = new double[]{1.0,2.1,3.1} and run the program.
You will observer its in real not converting into int. The output you will get is {"doubles":[1,2.1,3.1]}
All numbers are floats in Javascript. So, 1.0 and 1 are the same in JS. There is no differenciation of int, float and double.
Since JSON is going to end up as a JS object, there is no use in adding an extra '.0' since '1' represents a float as well. I guess this is done to save a few bytes in the string that is passed around.
So, you will get a float in JS, and if you parse it back to Java, you should get a double. Try it.
In the mean time, if you are interested in the way it displays on screen, you can try some string formatting to make it look like '1.0'.
"{"unitfactor":"0.1","unit":"N"}"
its because your unitfactor 0.1 is String in the making. change it to this:
"{"unitfactor":0.1,"unit":"N"}"
Remove the double quotation mark on 0.1.
or try to use the approach of @iNan:
double mUnitFactor = Double.parseDouble(fbeObject.getString("unitfactor"));
In this approach, you will first get the String unitfactor value, which is 0.1, and then you will parse it to Double using its wrapper class.
Since unitfactor is of type string, If you cannot change JSON then you can use
double mUnitFactor = Double.parseDouble(fbeObject.getString("unitfactor"))