Avoid currency Float or Double, lose precision. Recommended BigDecimal
scenes to be used
Recently a small program payment capabilities, unified under a single interface to provide micro-letter amounts of points, so the amount needs to be converted from yuan points
Initially calculated using Float
public static void main(String[] args) { //0.01元 String fee = "0.01"; //转为1分 int money = (int)(Float.parseFloat(fee)*100); System.out.println(money); }
0.01 yuan is no problem, for a larger amount of "9999999", the result is calculated 9999998.72
So instead BigDecimal
public static void main (String [] args) { // Amount Unit membered String Fee = "9999999" ; the BigDecimal decimal1 = new new the BigDecimal (Fee); the BigDecimal decimal2 = new new the BigDecimal (100 ); // Amount divided into units int Money = decimal1.multiply (decimal2) .intValue (); System.out.println (Money); // 9999999 }