Method 1:
The DecimalFormat returned in string format. This class is fully encapsulated in decimal. Like the% number, thousand bits, decimal precision. Scientific calculation.
Float Perprice=float.parsefloat (Textview_price_shopcar.gettext (). toString ()) * count;//string type converted to Float type
DecimalFormat decimalformat=new DecimalFormat (". 00");//character format for construction methods if the decimal place is less than 2 bits, it will be 0.//Reserved Two decimal places
String P=decimalformat.format (Perprice);//format returns a string
Method 2: Use Math.Round to calculate the number format that is returned here.
1234 |
float price= 89.89 ; int itemNum= 3 ; float totalPrice=price*itemNum; float num=( float )(Math.round(totalPrice* 100 )/ 100 ); //如果要求精确4位就*10000然后/10000 |
String converted to float and reserved two decimal places, shopping cart calculated price