MoneyUtil.java 699 B

123456789101112131415161718192021222324252627282930
  1. package cn.efunbox.base.util;
  2. import java.math.BigDecimal;
  3. import java.text.DecimalFormat;
  4. /**
  5. * MoneyUtil
  6. * Created by xusq on 2020/1/6.
  7. */
  8. public class MoneyUtil {
  9. /**
  10. * 元转分,确保price保留两位有效数字
  11. * @return
  12. */
  13. public static long changeY2F(double price) {
  14. DecimalFormat df = new DecimalFormat("#.00");
  15. price = Double.valueOf(df.format(price));
  16. long money = (long)price * 100;
  17. return money;
  18. }
  19. /**
  20. * 分转元,转换为bigDecimal在toString
  21. * @return
  22. */
  23. public static String changeF2Y(long price) {
  24. return BigDecimal.valueOf(price).divide(new BigDecimal(100)).toString();
  25. }
  26. }