< H1 > use the BigDecimal data type as follows: < / H1 >
public class MoneyUtil {
/**
* Translates the String repre sentation of a BigDecimal into a BigDecimal.
*/
private static final BigDecimal ONE_HUNDRED = new BigDecimal("100");
/**
*
* @param fen
* @return
*/
public static String fenToYuan(String fen){
// ,;
// ,
BigDecimal divide = new BigDecimal(fen).divide(ONE_HUNDRED, 2, 7);
return divide.toString();
}
/**
*
* @param yuan
* @return
*/
public static String yuanToFen(String yuan){
BigDecimal multiply = new BigDecimal(yuan).multiply(ONE_HUNDRED);
return String.valueOf(multiply.intValue());
}
}
< H1 > so it's OK to store String type in your database. < / H1 >
you can subtract and then divide. (88-5) / 100
you can use the large number type of Java, for example, in BigDecimal
database, you can store it as decimal type
money will generally use BigDecimal
to intercept it by String.
after getting the data inventory figures, use BigDecimal
BigDecimal price1 = new BigDecimal(88 / 100.0);
price1 = price1.setScale(2, BigDecimal.ROUND_HALF_DOWN);
BigDecimal price2 = new BigDecimal(5 / 100.0);
price2 = price2.setScale(2, BigDecimal.ROUND_HALF_DOWN);
BigDecimal res = price1.subtract(price2);
System.out.println(res);
The
BigDecimal.setScale () method is used to format the decimal point
setScale (1) to retain one decimal place. By default, the excess decimal places are deleted directly by rounding
setScale (1Magi BigDecimal.ROUNDDown). For example, 2.35becomes 2.3BigDecimal.ROUNDplaceUP rounding processing, and 2.35becomes 2.4BigDecimal.ROUNDplaceUp) rounding. 2.35 becomes 2.3, if it is 5, then rounding down
setScale (1MagneticBigDecimal.ROUNDgroundCEILING) is close to positive infinity rounding
setScale (1mceBigDecimal.ROUndeveloping floor) approaching negative infinity, the number > 0 is the same as ROUND_UP, and the number < 0 is the same as ROUND_DOWN
rounding off to the nearest number, if it is with two numbers.