double 类型转化为Integer类型 ---DecimalFormat
假设x是你要转换的double类型变量:
不进行四舍五入操作: (int)x
进行四舍五入操作: Integer.parseInt(new java.text.DecimalFormat("0").format(x));
DecimalFormat 是 NumberFormat 的一个具体子类,用于格式化十进制数字。该类设计有各种功能,使其能够分析和格式化任意语言环境中的数,包括对西方语言、阿拉伯语和印度语数字的支持。它还支持不同类型的数,包括整数 (123)、定点数 (123.4)、科学记数法表示的数 (1.23E4)、百分数 (12%) 和金额 ($123)。所有这些内容都可以本地化。
DecimalFormat 包含一个模式 和一组符号
符号含义:
符号 位置 本地化? 含义
0 数字 是 阿拉伯数字
# 数字字 是 阿拉伯数字,如果不存在则显示为空
. 数字 是 小数分隔符或货币小数分隔符
- 数字 是 减号
, 数字 是 分组分隔符
E 数字 是 分隔科学计数法中的尾数和指数。在前缀或后缀中无需加引号。
; 子模式边界 是 分隔正数和负数子模式
% 前缀或后缀 是 乘以 100 并显示为百分数
/u2030 前缀或后缀 是 乘以 1000 并显示为千分数
¤(/u00A4) 前缀或后缀 否 货币记号,由货币符号替换。如果两个同时出现,则用国际货币符号替换。如果出现在某个模式中,则使用货币小数分隔符,而不使用小数分隔符。
' 前缀或后缀 否 用于在前缀或或后缀中为特殊字符加引号,例如 "'#'#" 将 123 格式化为 "#123"。要创建单引号本身,请连续使用两个单引号:"# o''clock"。
例子:
[java] view plain copy
DecimalFormat df1 = new DecimalFormat("0.0");
DecimalFormat df2 = new DecimalFormat("#.#");
DecimalFormat df3 = new DecimalFormat("000.000");
DecimalFormat df4 = new DecimalFormat("###.###");
System.out.println(df1.format(12.34));
System.out.println(df2.format(12.34));
System.out.println(df3.format(12.34));
System.out.println(df4.format(12.34));
运行结果:
12.3
12.3
012.340
12.34
[java] view plain copy
DecimalFormat format = new DecimalFormat("###,####.000");
System.out.println(format.format(111111123456.1227222));
Locale.setDefault(Locale.US);
DecimalFormat usFormat = new DecimalFormat("###,###.000");
System.out.println(usFormat.format(111111123456.1227222));
DecimalFormat addPattenFormat = new DecimalFormat();
addPattenFormat.applyPattern("##,###.000");
System.out.println(addPattenFormat.format(111111123456.1227));
DecimalFormat zhiFormat = new DecimalFormat();
zhiFormat.applyPattern("0.000E0000");
System.out.println(zhiFormat.format(10000));
System.out.println(zhiFormat.format(12345678.345));
DecimalFormat percentFormat = new DecimalFormat();
percentFormat.applyPattern("#0.000%");
System.out.println(percentFormat.format(0.3052222));
运行结果 :
1111,1112,3456.123
111,111,123,456.123
111,111,123,456.123
1.000E0004
1.235E0007
30.522%
如果使用具有多个分组字符的模式,则最后一个分隔符和整数结尾之间的间隔才是使用的分组大小。所以 "#,##,###,####" == "######,####" == "##,####,####"。
double 类型转化为Integer类型 ---DecimalFormat的更多相关文章
- double 类型转化为Integer
(1)把double先转化成int类型 Double reseve3=Double.parseDouble(bddet[0].getReserve3()); int b=reseve3.intValu ...
- Java 中要将 String 类型转化为 int 类型
在 Java 中要将 String 类型转化为 int 类型时,需要使用 Integer 类中的 parseInt() 方法或者 valueOf() 方法进行转换. 例1: 1 2 3 4 5 6 S ...
- 将字符串类型转化为date类型
直接上代码 import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; pub ...
- asp.net如何把一个tostring类型转化为dateTime类型
Convert.ToDateTime(dr["consult_DealTime"].ToString()).ToString("yyyy-MM-dd"); co ...
- C++中将string类型转化为int类型
写程序需要将string转化为int,所以就探索了一下. 方法一:atoi函数 atoi函数将字符串转化为整数,注意需要stdlib库.所以就尝试了一下: #include <iostream& ...
- sqoop将mysql的tinyint类型转化为boolean类型
当mysql中的字段为tinyint类型时,通过sqoop导入数据到hdfs上,在hdfs上显示的是true,false值.为了显示0,1. 解决方法: jdbc:mysql://<hostna ...
- JSTL将number类型转化为String类型
<c:set var="lm1"> <c:out value="${lm}" /> </c:set>
- number类型转化为string类型
toString 方法 string = toString(num) 缺点: 不能转化 underfind 和 null 2 String 方法 string = String(num) 可以转化 u ...
- Java中Object转化为int类型
转自:http://blog.sina.com.cn/s/blog_5f8421fb010162kb.html Java中由Object类型转化为int类型时,不能直接转化,先是将Object类型转化 ...
随机推荐
- 一次流量稍高导致web项目慢的问题&解决
项目上线后,用户越来越多,有一天出现一个issue:用户访问特别慢. 首先介绍下架构: haproxy/Nginx / \ node1 node2 | | redis redis(slave) | | ...
- Domain Space
Bluehost Register Page http://www.bluehost.com/track/weipengp
- 查看、关闭linux自启动网络服务
1.查看 netstat --tulnp ..master smtp 服务 2.关闭 /etc/init.d/服务 stop 停止 : start 启动 chkconfig 服务 off 关闭 ...
- [转]android开发之字节顺序
原文在此 android上C++程序为小端字节顺序,和windows上一样. 而android上的JAVA程序则使用的是大端字节顺序. 用NDK和java SDK 做android程序时发现的问题,记 ...
- 在CodedUI中使用JQuery选择器
在CodedUI中使用JQuery选择器http://automationqa.com/forum.php?mod=viewthread&tid=3574&fromuid=29
- 微信公众号API返回不可打印字符的处理。
引言 在使用微信公众号的"批量获取用户基本信息"API时,发现在某些时候不能使用php的json_decode转为数组,打印出curl获取到的内容中,发现了几个不可打印字符.相信不 ...
- ps图像渐变
整理自:http://zhidao.baidu.com/question/16374167.html 1.用ps打开图片 2.在切换至英文输入法状态下(下面的操作均如此)按q 快捷键q的作用是切换标准 ...
- Django:Model的Filter
转自:http://www.douban.com/note/301166150/ django model filter 条件过滤,及多表连接查询.反向查询,某字段的distinct 1.多表 ...
- CM: 如何自己build一个官方版本出来,使用官方release中的manifests.xml
This can be accomplished in one of two ways: 1) Pull the file from your device:cd /your/repo/pathadb ...
- DirectWrite文字排版——字符串去尾
DirectWrite是 DirectX 家族中专门用来做文本处理的部分,主要配合Direct2D进行渲染工作. 一.字符串去尾介绍 在文字渲染中,不免会遇到字符串去尾的需求.字符串去尾指的是:当字符 ...