本文主要是介绍JAVA元转换成分,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
代码如下:
/**** 将元为单位的转换为分 (乘100)** @param amount* @return*/public static String changeBranch(Long amount) {return BigDecimal.valueOf(amount).multiply(new BigDecimal(100)).toString();}/*** 将元为单位的转换为分 替换小数点,支持以逗号区分的金额** @param amount* @return*/public static String changeBranch(String amount) {String currency = amount.replaceAll("\\$|\\¥|\\,", ""); // 处理包含, ¥// 或者$的金额int index = currency.indexOf(".");int length = currency.length();Long amLong;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();}public static void main(String[] args) {System.out.println("元-分==" + changeBranch("0.01"));System.out.println("元-分==" + changeBranch("100"));System.out.println("元-分==" + changeBranch((long) 100));}
这篇关于JAVA元转换成分的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!