| package com.dayu.recharge.utils; | 
|   | 
| import java.math.BigDecimal; | 
| import java.text.DecimalFormat; | 
| import java.text.NumberFormat; | 
| import java.text.ParseException; | 
|   | 
| /** | 
|  * Created by zuoxiao on 2018/12/25. | 
|  */ | 
|   | 
| public class ArithUtil { | 
|   | 
|     public static double add(double v1, double v2) { | 
|         BigDecimal b1 = new BigDecimal(Double.toString(v1)); | 
|         BigDecimal b2 = new BigDecimal(Double.toString(v2)); | 
|         return b1.add(b2).doubleValue(); | 
|     } | 
|   | 
|     public static String jiaoToYuan(String amount) { | 
|         NumberFormat format = NumberFormat.getInstance(); | 
|         try { | 
|             Number number = format.parse(amount); | 
|             double temp = number.doubleValue() / 10.0; | 
|             format.setGroupingUsed(false); | 
|             // 设置返回的小数部分所允许的最大位数 | 
|             format.setMaximumFractionDigits(1); | 
|             amount = format.format(temp); | 
|         } catch (ParseException e) { | 
|             e.printStackTrace(); | 
|         } | 
|         return amount; | 
|     } | 
|   | 
|   | 
|     /** | 
|      * 元转分,确保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(String price) { | 
|         NumberFormat format = NumberFormat.getInstance(); | 
|         try { | 
|             Number number = format.parse(price); | 
|             double temp = number.doubleValue() / 100.0; | 
|             format.setGroupingUsed(false); | 
|             // 设置返回的小数部分所允许的最大位数 | 
|             format.setMaximumFractionDigits(2); | 
|             format.setMinimumFractionDigits(2); | 
|             price = format.format(temp); | 
|         } catch (ParseException e) { | 
|             e.printStackTrace(); | 
|         } | 
|         return price; | 
|   | 
|     } | 
|   | 
|     public static void main(String[] args) { | 
|         changeF2Y("100"); | 
|         changeY2F("25"); | 
|     } | 
|   | 
|     /** | 
|      * 将元为单位的转换为分 替换小数点,支持以逗号区分的金额 | 
|      * | 
|      * @param amount | 
|      * @return | 
|      */ | 
|     public static String changeY2F(String amount) { | 
|         String currency = amount.replaceAll("\\$|\\¥|\\,", "");  //处理包含, ¥ 或者$的金额 | 
|         int index = currency.indexOf("."); | 
|         int length = currency.length(); | 
|         Long amLong = 0l; | 
|         if (index == -1) { | 
|             amLong = Long.valueOf(currency + "00"); | 
|         } else if (length - index >= 3) { | 
|             amLong = Long.valueOf((currency.substring(0, index + 3)).replace(".", "")); | 
|         } else if (length - index == 2) { | 
|             amLong = Long.valueOf((currency.substring(0, index + 2)).replace(".", "") + 0); | 
|         } else { | 
|             amLong = Long.valueOf((currency.substring(0, index + 1)).replace(".", "") + "00"); | 
|         } | 
|         return amLong.toString(); | 
|     } | 
|   | 
| } |