新聞中心
這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
如何在java項(xiàng)目中實(shí)現(xiàn)分轉(zhuǎn)元與元轉(zhuǎn)分操作-創(chuàng)新互聯(lián)
這篇文章給大家介紹如何在java項(xiàng)目中實(shí)現(xiàn)分轉(zhuǎn)元與元轉(zhuǎn)分操作,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。
分轉(zhuǎn)元
private String fenToYuan(String amount){ NumberFormat format = NumberFormat.getInstance(); try{ Number number = format.parse(amount); double temp = number.doubleValue() / 100.0; format.setGroupingUsed(false); // 設(shè)置返回的小數(shù)部分所允許的較大位數(shù) format.setMaximumFractionDigits(2); amount = format.format(temp); } catch (ParseException e){ e.printStackTrace(); } return amount; }
轉(zhuǎn)換為分
private String yuanToFen(String amount){ NumberFormat format = NumberFormat.getInstance(); try{ Number number = format.parse(amount); double temp = number.doubleValue() * 100.0; format.setGroupingUsed(false); // 設(shè)置返回?cái)?shù)的小數(shù)部分所允許的較大位數(shù) format.setMaximumFractionDigits(0); amount = format.format(temp); } catch (ParseException e){ e.PrintStackTrace(); } return amount; }
補(bǔ)充:java 分轉(zhuǎn)元 decimal格式化
我就廢話不多說了,大家還是直接看代碼吧~
public class NumberUtils { public static String format(double f) { DecimalFormat df = new DecimalFormat("#.##"); return df.format(f) ; } public static String fenToYuan(int f) { return format(f/100.0) ; } public static void main(String[] args) { System.out.println(fenToYuan(3)); System.out.println(fenToYuan(33)); System.out.println(fenToYuan(333)); System.out.println(fenToYuan(3333)); System.out.println(fenToYuan(33333)); } }
關(guān)于如何在java項(xiàng)目中實(shí)現(xiàn)分轉(zhuǎn)元與元轉(zhuǎn)分操作就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。
新聞標(biāo)題:如何在java項(xiàng)目中實(shí)現(xiàn)分轉(zhuǎn)元與元轉(zhuǎn)分操作-創(chuàng)新互聯(lián)
網(wǎng)頁(yè)地址:http://www.ef60e0e.cn/article/echip.html