zoukankan      html  css  js  c++  java
  • JAVA 金额自动除以100,精确到分

    package net.crisps.cloud.order.commons.staff.util;

    import com.fasterxml.jackson.core.JsonGenerator;
    import com.fasterxml.jackson.databind.JsonSerializer;
    import com.fasterxml.jackson.databind.SerializerProvider;
    import org.springframework.stereotype.Component;

    import java.io.IOException;
    import java.math.BigDecimal;
    import java.text.DecimalFormat;
    import java.util.Objects;

    1.编写一个工具类继承 JsonSerializer

    @Component
    public class MoneyUtils extends JsonSerializer<Long> {

    @Override
    public void serialize(Long aLong, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
    if (Objects.nonNull(aLong)) {
    String format = getString(aLong);
    jsonGenerator.writeString(format);
    } else {//这个分支不要忘记了,否则将不输出这个属性的值
    aLong = 0L;
    String format = getString(aLong);
    jsonGenerator.writeString(format);
    }
    }

    private String getString(Long aLong) {
    DecimalFormat df = new DecimalFormat("0.00");
    return df.format(aLong.doubleValue() / 100);
    }
    }

    2. 再返回的实体上加上注解

    3. 然后看返回数据




  • 相关阅读:
    tesseract的简单使用
    快速添加请求头
    1010. 一元多项式求导 (25)
    1009. 说反话 (20)
    1008. 数组元素循环右移问题 (20)
    1007. 素数对猜想 (20)
    1006. 换个格式输出整数 (15)
    素数判断
    1002. 写出这个数 (20)
    1005. 继续(3n+1)猜想 (25)
  • 原文地址:https://www.cnblogs.com/bt2882/p/14607694.html
Copyright © 2011-2022 走看看