元转分和分转元的java实现

 

  /**
     * 元转分,确保price保留两位有效数字
     * @return
     */
    public static int changeY2F(double price) {
        DecimalFormat df = new DecimalFormat("#.00");
        price = Double.valueOf(df.format(price));
        int money = (int)(price * 100);
        return money;
    }
    
    /**
     * 分转元,转换为bigDecimal在toString
     * @return
     */
    public static String changeF2Y(int price) {
        return BigDecimal.valueOf(Long.valueOf(price)).divide(new BigDecimal(100)).toString();
    }

 

posted @ 2018-08-27 10:07  大宇007  阅读(23057)  评论(0编辑  收藏  举报