首页 > 其他 > 详细

String转换成float并且保留两位小数,购物车计算价格

时间:2016-01-16 16:30:06      阅读:211      评论:0      收藏:0      [点我收藏+]

 方法1:

用DecimalFormat 返回的是String格式的.该类对十进制进行全面的封装.像%号,千分位,小数精度.科学计算.

  float perPrice=Float.parseFloat(textView_price_shopCar.getText().toString()) * count;//String类型转换成float类型

      DecimalFormat decimalFormat=new DecimalFormat(".00");//构造方法的字符格式这里如果小数不足2位,会以0补足.//保留两位小数

      String p=decimalFormat.format(perPrice);//format 返回的是字符串

方法2:用Math.round计算,这里返回的数字格式的.

 
1
2
3
4
float price=89.89;
int itemNum=3;
float totalPrice=price*itemNum;
float num=(float)(Math.round(totalPrice*100)/100);//如果要求精确4位就*10000然后/10000

String转换成float并且保留两位小数,购物车计算价格

原文:http://www.cnblogs.com/Jingerxin/p/5135491.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!