Java float保留两位小数或多位小数
Java float保留两位小数或多位小数
方法1:用Math.round计算,这里返回的数字格式的.
float price=89.89;
int itemNum=3;
float totalPrice=price*itemNum;
float num=(float)(Math.round(totalPrice*100)/100); //如果要求精确4位就*10000然后/10000
方法2(OK): 用DecimalFormat 返回的是String格式的.该类对十进制进行全面的封装.像%号,千分位,小数精度.科学计算.
import java.text.DecimalFormat;
float price=1.2;
DecimalFormat decimalFormat=new DecimalFormat("0.00"); //构造方法的字符格式这里如果小数不足2位,会以0补足.
String p=decimalFormat.format(price); //format 返回的是字符串
Java float保留两位小数或多位小数的更多相关文章
- [Java基础] Java float保留两位小数或多位小数
方法1:用Math.round计算,这里返回的数字格式的. float price=89.89; int itemNum=3; float totalPrice=price*itemNum; floa ...
- java double 保留两位小数
java保留两位小数问题: 方式一: 四舍五入 double f = 111231.5585; BigDecimal b = new BigDecimal(f); d ...
- float 保留两位小数
1.页面运算格式化数字 页面上有时候会用到数字的运算,运算过后会出现1.5999999999999这么长的数字,需要格式化数字,比如保留两位有效数字 首先导入这个标签 <%@ taglib ur ...
- java 四舍五入保留两位小数
// 保留两位小数 System.out.println(Double.parseDouble(String.format("%.2f", 55.5454545454))); // ...
- java 四舍五入 保留两位小数
1. 格式化字符串 java.text.DecimalFormat df = new java.text.DecimalFormat("#0.00"); float val=Flo ...
- java中保留两位小数的方法
1.BigDecimal的setScale // RoundingMode 舍入模式: // 1.UP/DOWN ->基准为数字0: // 2.CEILING/FLOOR ->基准为正负无 ...
- java数字保留两位小数四舍五入
import java.math.BigDecimal; import java.text.DecimalFormat; import java.text.NumberFormat; public c ...
- java四舍五入保留两位小数4种方法
4种方法,都是四舍五入,例: import java.math.BigDecimal; import java.text.DecimalFormat; import java.text.NumberF ...
- JAVA 保留两位小数
package com.oracle.pojo; import java.math.BigDecimal; import java.text.DecimalFormat; import java.te ...
随机推荐
- [Sinatra、Mongo] Mongo
Mongo is a document-oriented database. Install the required gems: gem install mongo gem install bson ...
- 给图像添加logo
#include <opencv2\opencv.hpp>#include"ProcessPixels.h"using namespace cv;using names ...
- Sql Server 检测死锁的SQL语句
首先创建一个标量值函数DigLock,用来递归检测SqlServer中的每一个会话是否存在加锁循环,如果该函数最终返回1则表示检测到了加锁循环 (也就是说检测到了死锁),如果最终返回0则表示没有检测到 ...
- mirrors
http://mirrors.163.com/http://mirrors.aliyun.com/http://mirrors.aliyun.com/centos/7.2.1511/os/x86_64 ...
- V4L2应用程序框架-二【转】
本文转载自:http://blog.csdn.net/tommy_wxie/article/details/11371439 V4L2驱动框架 主设备号: 81 次设备号: 0-63 64 ...
- 160912、工具类:spring+springmvc自定义编码转换
一.自定义的类(注意其中的属性,web.xml中的配置就是根据这个类的) import org.springframework.web.filter.OncePerRequestFilter; imp ...
- linux中断的上半部和下半部 【转】
转自:http://blog.chinaunix.net/xmlrpc.php?r=blog/article&uid=24690947&id=3491821 一.什么是下半部 中断是一 ...
- webapi获取请求地址的IP
References required: HttpContextWrapper - System.Web.dll RemoteEndpointMessageProperty - System.Serv ...
- html5 教程网站
html5 MDN Canvas tutorial Canvas教程 canvas: 阮一峰 在那山的那边海的那边有一群程序猿 使用 HTML5 canvas 绘制精美的图形 HTML5定稿了,为什么 ...
- centos环境源码安装postgresql9.4
源码安装简要步骤 下载PostgreSQL 源码包 下载根目录地址:http://ftp.postgresql.org/ 本人选择的是当前最新版本v9.4.1:http://ftp.postgre ...