金額分元轉換

2022-06-27 14:42:10 字數 1137 閱讀 9245

/**

* 元轉分,確保price保留兩位有效數字

* @return

*/public

static

intchangey2f(string amount)

/*** 分轉元,轉換為bigdecimal在tostring 保留2位小數

* @return

*/public

static string changef2y(int

price)

1、setscale(2)方法其實內部預設傳了 round_unnecessary,

表示結果是精確的值不需要捨入,如果結果的位數和入參不一致將報錯

public bigdecimal setscale(int newscale)

2、bigdecimal.round_down  直接捨掉多餘的部分

bigdecimal b = new bigdecimal("2.4599999").setscale(2, bigdecimal.round_down);// 結果2.45

3、bigdecimal.round_up  捨棄部分》0就進1

bigdecimal c = new bigdecimal("2.2300001").setscale(2, bigdecimal.round_up); //結果2.24

4、bigdecimal.round_half_up  捨棄部分》=5 進1

bigdecimal d = new bigdecimal("2.225").setscale(2, bigdecimal.round_half_up); //結果2.23

5、bigdecimal.round_half_down 捨棄部分》5進1

bigdecimal e = new bigdecimal("2.225").setscale(2, bigdecimal.round_half_down);//結果2.22

6、bigdecimal.round_ceiling 類同bigdecimal.round_up

7、bigdecimal.round_floor   類同bigdecimal.round_down

微信支付金額轉換(元轉分)

字串轉換的話可以很好的解決損失精度的問題 double money 100.99 bigdecimal money new bigdecimal 100.99 string money 100.99 money可以是字串,可以是double,可以是bigdecimal 型別 基本上傳過來的支付金額,...

金額 元與分之間轉換

public static void main string args 功能描述 金額字串轉換 單位分轉成單元 param str 傳入需要轉換的金額字串 return 轉換後的金額字串 public static string fentoyuan object o else if len 3 el...

元和分單位轉換

最近做的 系統涉及了很多元和分的轉化,之前有過分轉元 100和元 100發現這樣處理有些資料會丟失精度,下面是我寫的方法可以解決是精度問題 分轉元 penny2yuan function penny 排除小於1的數 if parseint penny 1 排除帶小數點的數 var penny str...