NumberFormat 是所有数值格式的抽象基类。 该类提供了格式化和分析数值的接口。 NumberFormat 也提供了确定

哪个语言环境具有数值格式以及它们名字的方法。

  1. import java.text.NumberFormat;
  2. public class Test {
  3. public static void main(String[] args) {
  4. // TODO Auto-generated method stub
  5. Double myNumber=23323.3323232323;
  6. Double test=0.3434;
  7. //getInstance()
  8. //返回当前缺省语言环境的缺省数值格式。
  9. String myString = NumberFormat.getInstance().format(myNumber);
  10. System.out.println(myString);
  11. //getCurrencyInstance()返回当前缺省语言环境的通用格式
  12. myString = NumberFormat.getCurrencyInstance().format(myNumber);
  13. System.out.println(myString);
  14. //getNumberInstance() 返回当前缺省语言环境的通用数值格式。
  15. myString = NumberFormat.getNumberInstance().format(myNumber);
  16. System.out.println(myString);
  17. //getPercentInstance()  返回当前缺省语言环境的百分比格式。
  18. myString = NumberFormat.getPercentInstance().format(test);
  19. System.out.println(myString);
  20. //setMaximumFractionDigits(int) 设置数值的小数部分允许的最大位数。
  21. //setMaximumIntegerDigits(int)  设置数值的整数部分允许的最大位数。
  22. //setMinimumFractionDigits(int) 设置数值的小数部分允许的最小位数。
  23. //setMinimumIntegerDigits(int)  设置数值的整数部分允许的最小位数.
  24. NumberFormat format = NumberFormat.getInstance();
  25. format.setMinimumFractionDigits( 3 );
  26. format.setMaximumFractionDigits(5);
  27. format.setMaximumIntegerDigits( 10 );
  28. format.setMinimumIntegerDigits(0);
  29. System.out.println(format.format(2132323213.23266666666));
  30. }
  31. }
  1. import java.text.DecimalFormat;
  2. import java.util.Random;
  3. import java.util.Locale;
  4. public class TestNumberFormat {
  5. public static void main(String[] args) {
  6. double pi = 3.1415927;// 圆周率
  7. // 取一位整数
  8. System.out.println(new DecimalFormat("0").format(pi)); // 3
  9. // 取一位整数和两位小数
  10. System.out.println(new DecimalFormat("0.00").format(pi)); // 3.14
  11. // 取两位整数和三位小数,整数不足部分以0填补。
  12. System.out.println(new DecimalFormat("00.000").format(pi)); // 03.142
  13. // 取所有整数部分
  14. System.out.println(new DecimalFormat("#").format(pi)); // 3
  15. // 以百分比方式计数,并取两位小数
  16. System.out.println(new DecimalFormat("#.##%").format(pi)); // 314.16%
  17. long c = 299792458;// 光速
  18. // 显示为科学计数法,并取五位小数
  19. System.out.println(new DecimalFormat("#.#####E0").format(c)); // 2.99792E8
  20. // 显示为两位整数的科学计数法,并取四位小数
  21. System.out.println(new DecimalFormat("00.####E0").format(c)); // 29.9792E7
  22. // 每三位以逗号进行分隔。
  23. System.out.println(new DecimalFormat(",###").format(c)); // 299,792,458
  24. // 将格式嵌入文本
  25. System.out.println(new DecimalFormat("光速大小为每秒,###米。").format(c));        //光速大小为每秒299,792,458米。
  26. //直接截取
  27. System.out.println(Math.floor(24.335*100)/100);            //24.33
  28. System.out.println((int)Math.floor(23.45));        //截取整数    23
  29. System.out.println((int)Math.ceil(23.2));        //凑整    24
  30. System.out.println((int)Math.rint(23.567));        //四舍五入取整    24
  31. System.out.println(new DecimalFormat("0").format(25.5));        //四舍五入取整    26
  32. System.out.println(Math.random());        //随机double 0.9786833725139645
  33. System.out.println(new Random().nextFloat());        //随机浮点数 0.5196178
  34. System.out.println(new Random().nextBoolean());        //随机boolean
  35. //        Locale.setDefault(Locale.ENGLISH);
  36. //        DecimalFormat df = new DecimalFormat();
  37. //        df.applyPattern("00.00");
  38. //        System.out.println(df.format(4.2355));
  39. }
  40. }

NumberFormat usage的更多相关文章

  1. Java Numeric Formatting--reference

    I can think of numerous times when I have seen others write unnecessary Java code and I have written ...

  2. intellij IDEA 出现“Usage of API documented as @since 1.6+”的解决办法

    问题 在导入java.io.console的时候出现"Usage of API documented as @since 1.6+"

  3. Disk Space Usage 术语理解:unallocated, unused and reserved

    通过standard reports查看Disk Usage,选中Database,右击,选择Reports->Standard Reports->Disk Space Usage,截图如 ...

  4. OpenCascade MeshVS Usage

    OpenCascade MeshVS Usage eryar@163.com Abstract. MeshVS means Mesh Visualization Service. It can be ...

  5. NumberFormat DecimalFormat

    http://blog.csdn.net/evangel_z/article/details/7624503 http://blog.163.com/wangzhengquan85@126/blog/ ...

  6. Usage: AddDimensionedImage imageFile outputFile eclipse 运行程序出错

    关于这个在eclipse中运行java程序的错,首先确认你的jdk,jre是否完整,并且与你的eclipse的位数相同,当然我相信这个错误大家应该都会去检查到. 第二个关于addDimensioned ...

  7. Please allow Subclipse team to receive anonymous usage statistics for this Eclipse intance(info)

    本文转载自:http://blog.csdn.net/myfxx/article/details/21096949 今天在用eclipse启动项目的时候发现了一个问题,就是每次启动项目的时候,ecli ...

  8. [转]Dynamic SQL & Stored Procedure Usage in T-SQL

    转自:http://www.sqlusa.com/bestpractices/training/scripts/dynamicsql/ Dynamic SQL & Stored Procedu ...

  9. 处理Linux下subversion尝试连接自建的VisualSVN server报“Key usage violation in certificate has been detected”错误的问题

    在Linux下使用subversion尝试链接VisualSVN server搭建的svn库,可能会报下面错误, svn: OPTIONS of 'https://server.domain.loca ...

随机推荐

  1. 22. Surrounded Regions

    Surrounded Regions Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A ...

  2. 小学了一下css hack

    实例讲解: Hack应用情境(一) 适用范围:IE:6.0,IE7.0,IE8.0之间的兼容 实例说明: 使用了渐进识别的方式,从总体中逐渐排除局部.首先,巧妙的使用“\9”这一标记,将IE游览器从所 ...

  3. Dynamics Webservice Call with Credential

    Dynamics Webservice call with credential /// <summary> ///WebServiceHelper 的摘要说明 /// </summ ...

  4. java 代码判断图片格式后缀名称

    /** * 图片判断 */ private static String getFormatName(Object o) { try { // Create an image input stream ...

  5. 关于页面 reflow 和 repaint

    什么是 reflow 和 repaint 浏览器为了重新渲染部分或整个页面,重新计算页面元素位置和几何结构(geometries)的进程叫做 reflow. 当确定了元素位置.大小以及其他属性,例如颜 ...

  6. js复制input 框中的值

    function copy(){ var Url2=document.getElementById("copyValue"); Url2.select(); document.ex ...

  7. Analyze network packet files very carefully

    As a professional forensic guy, you can not be too careful to anlyze the evidence. Especially when t ...

  8. CentOS网络配置详解

    转载于CentOS中文站:http://www.centoscn.com/CentOS/2015/0507/5376.html一.配置文件详解 在RHEL或者CentOS等Redhat系的Linux系 ...

  9. 理解angularjs的作用域

    <!doctype html> <html ng-app="myApp"> <head> <script src="http:/ ...

  10. {Reship}{ListView}C# ListView用法详解

    ======================================================================== This aritcle came from http ...