00 as decimal places when serialize Double
- Dominant language
- Java
- Stars
- 24.2k
- Forks
- 4.5k
- Avg merge
- 6d 4h
- Merged PRs (30d)
- 12
Description
Hi team,
For a specific reason, I want 2 decimal places for my double when serializing into JSON.
Something like:
9900 --> 9900.00
9900.0 --> 9900.00
9900.1234 --> 9900.12
My best result so far:
9900 --> 9900.0
9900.0 --> 9900.0
9900.1234 --> 9900.12
My DoubleAdapter.java, please advice. Thanks a lot!
```
class DoubleAdapter extends TypeAdapter {
@Override
public void write(JsonWriter jsonWriter, Double aDouble) throws IOException {
DecimalFormat df = new DecimalFormat("0.00");
jsonWriter.value(Double.valueOf(df.format(aDouble)));
}
@Override
public Double read(JsonReader jsonReader) throws IOException {
return jsonReader.nextDouble();
}
}
```
Contributor guide
Assessment
This issue has not been assessed yet.