批处理处理金额小数点问题
NumberAdapter自定义转换器
/** * 用来处理小数点问题 */ public class NumberAdapter extends TypeAdapter<String> { @Override public void write(JsonWriter out, String value) throws IOException { if (value == null) { out.nullValue(); } else if (value.isEmpty()) { out.value("0"); } else { out.value(value); } } @Override public String read(JsonReader in) throws IOException { JsonToken peek = in.peek(); if (peek == JsonToken.NULL) { in.nextNull(); return "0"; } String value = in.nextString(); BigDecimal bigDecimal = new BigDecimal(value); BigDecimal zero = BigDecimal.ZERO; if (bigDecimal.compareTo(zero) == 0) { return "0"; } String result = bigDecimal.setScale(2, RoundingMode.DOWN).stripTrailingZeros().toPlainString(); // Log.i("NumberAdapter", result); return result; } }
bean类使用
@JsonAdapter(NumberAdapter.class) private String money;