NumberFormat usage
NumberFormat 是所有数值格式的抽象基类。 该类提供了格式化和分析数值的接口。 NumberFormat 也提供了确定
哪个语言环境具有数值格式以及它们名字的方法。
- import java.text.NumberFormat;
- public class Test {
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- Double myNumber=23323.3323232323;
- Double test=0.3434;
- //getInstance()
- //返回当前缺省语言环境的缺省数值格式。
- String myString = NumberFormat.getInstance().format(myNumber);
- System.out.println(myString);
- //getCurrencyInstance()返回当前缺省语言环境的通用格式
- myString = NumberFormat.getCurrencyInstance().format(myNumber);
- System.out.println(myString);
- //getNumberInstance() 返回当前缺省语言环境的通用数值格式。
- myString = NumberFormat.getNumberInstance().format(myNumber);
- System.out.println(myString);
- //getPercentInstance() 返回当前缺省语言环境的百分比格式。
- myString = NumberFormat.getPercentInstance().format(test);
- System.out.println(myString);
- //setMaximumFractionDigits(int) 设置数值的小数部分允许的最大位数。
- //setMaximumIntegerDigits(int) 设置数值的整数部分允许的最大位数。
- //setMinimumFractionDigits(int) 设置数值的小数部分允许的最小位数。
- //setMinimumIntegerDigits(int) 设置数值的整数部分允许的最小位数.
- NumberFormat format = NumberFormat.getInstance();
- format.setMinimumFractionDigits( 3 );
- format.setMaximumFractionDigits(5);
- format.setMaximumIntegerDigits( 10 );
- format.setMinimumIntegerDigits(0);
- System.out.println(format.format(2132323213.23266666666));
- }
- }
- import java.text.DecimalFormat;
- import java.util.Random;
- import java.util.Locale;
- public class TestNumberFormat {
- public static void main(String[] args) {
- double pi = 3.1415927;// 圆周率
- // 取一位整数
- System.out.println(new DecimalFormat("0").format(pi)); // 3
- // 取一位整数和两位小数
- System.out.println(new DecimalFormat("0.00").format(pi)); // 3.14
- // 取两位整数和三位小数,整数不足部分以0填补。
- System.out.println(new DecimalFormat("00.000").format(pi)); // 03.142
- // 取所有整数部分
- System.out.println(new DecimalFormat("#").format(pi)); // 3
- // 以百分比方式计数,并取两位小数
- System.out.println(new DecimalFormat("#.##%").format(pi)); // 314.16%
- long c = 299792458;// 光速
- // 显示为科学计数法,并取五位小数
- System.out.println(new DecimalFormat("#.#####E0").format(c)); // 2.99792E8
- // 显示为两位整数的科学计数法,并取四位小数
- System.out.println(new DecimalFormat("00.####E0").format(c)); // 29.9792E7
- // 每三位以逗号进行分隔。
- System.out.println(new DecimalFormat(",###").format(c)); // 299,792,458
- // 将格式嵌入文本
- System.out.println(new DecimalFormat("光速大小为每秒,###米。").format(c)); //光速大小为每秒299,792,458米。
- //直接截取
- System.out.println(Math.floor(24.335*100)/100); //24.33
- System.out.println((int)Math.floor(23.45)); //截取整数 23
- System.out.println((int)Math.ceil(23.2)); //凑整 24
- System.out.println((int)Math.rint(23.567)); //四舍五入取整 24
- System.out.println(new DecimalFormat("0").format(25.5)); //四舍五入取整 26
- System.out.println(Math.random()); //随机double 0.9786833725139645
- System.out.println(new Random().nextFloat()); //随机浮点数 0.5196178
- System.out.println(new Random().nextBoolean()); //随机boolean
- // Locale.setDefault(Locale.ENGLISH);
- // DecimalFormat df = new DecimalFormat();
- // df.applyPattern("00.00");
- // System.out.println(df.format(4.2355));
- }
- }
NumberFormat usage的更多相关文章
- Java Numeric Formatting--reference
I can think of numerous times when I have seen others write unnecessary Java code and I have written ...
- intellij IDEA 出现“Usage of API documented as @since 1.6+”的解决办法
问题 在导入java.io.console的时候出现"Usage of API documented as @since 1.6+"
- Disk Space Usage 术语理解:unallocated, unused and reserved
通过standard reports查看Disk Usage,选中Database,右击,选择Reports->Standard Reports->Disk Space Usage,截图如 ...
- OpenCascade MeshVS Usage
OpenCascade MeshVS Usage eryar@163.com Abstract. MeshVS means Mesh Visualization Service. It can be ...
- NumberFormat DecimalFormat
http://blog.csdn.net/evangel_z/article/details/7624503 http://blog.163.com/wangzhengquan85@126/blog/ ...
- Usage: AddDimensionedImage imageFile outputFile eclipse 运行程序出错
关于这个在eclipse中运行java程序的错,首先确认你的jdk,jre是否完整,并且与你的eclipse的位数相同,当然我相信这个错误大家应该都会去检查到. 第二个关于addDimensioned ...
- Please allow Subclipse team to receive anonymous usage statistics for this Eclipse intance(info)
本文转载自:http://blog.csdn.net/myfxx/article/details/21096949 今天在用eclipse启动项目的时候发现了一个问题,就是每次启动项目的时候,ecli ...
- [转]Dynamic SQL & Stored Procedure Usage in T-SQL
转自:http://www.sqlusa.com/bestpractices/training/scripts/dynamicsql/ Dynamic SQL & Stored Procedu ...
- 处理Linux下subversion尝试连接自建的VisualSVN server报“Key usage violation in certificate has been detected”错误的问题
在Linux下使用subversion尝试链接VisualSVN server搭建的svn库,可能会报下面错误, svn: OPTIONS of 'https://server.domain.loca ...
随机推荐
- 22. Surrounded Regions
Surrounded Regions Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A ...
- 小学了一下css hack
实例讲解: Hack应用情境(一) 适用范围:IE:6.0,IE7.0,IE8.0之间的兼容 实例说明: 使用了渐进识别的方式,从总体中逐渐排除局部.首先,巧妙的使用“\9”这一标记,将IE游览器从所 ...
- Dynamics Webservice Call with Credential
Dynamics Webservice call with credential /// <summary> ///WebServiceHelper 的摘要说明 /// </summ ...
- java 代码判断图片格式后缀名称
/** * 图片判断 */ private static String getFormatName(Object o) { try { // Create an image input stream ...
- 关于页面 reflow 和 repaint
什么是 reflow 和 repaint 浏览器为了重新渲染部分或整个页面,重新计算页面元素位置和几何结构(geometries)的进程叫做 reflow. 当确定了元素位置.大小以及其他属性,例如颜 ...
- js复制input 框中的值
function copy(){ var Url2=document.getElementById("copyValue"); Url2.select(); document.ex ...
- Analyze network packet files very carefully
As a professional forensic guy, you can not be too careful to anlyze the evidence. Especially when t ...
- CentOS网络配置详解
转载于CentOS中文站:http://www.centoscn.com/CentOS/2015/0507/5376.html一.配置文件详解 在RHEL或者CentOS等Redhat系的Linux系 ...
- 理解angularjs的作用域
<!doctype html> <html ng-app="myApp"> <head> <script src="http:/ ...
- {Reship}{ListView}C# ListView用法详解
======================================================================== This aritcle came from http ...