使用MessageFormat格式化数字,有一个很隐蔽的技巧点:

public static void main(String[] args) {
MessageFormat mf = new MessageFormat("{0,number,#.##},{0,number,#.#}");
Object[] objs = {new Double(3.1415)};
String result = mf.format(objs);
// result now equals "3.14,3.1"
System.out.println(result);
}

程序输出:3.14,3.1

上述代码修改一处:

//#.##前后加2个空格
MessageFormat mf = new MessageFormat("{0,number, #.## },{0,number,#.#}");

程序输出:  3.14  ,3.1

"#.##"字符串的格式,会影响格式化的结果。

MessageFormat格式化数字的更多相关文章

  1. java.text.MessageFormat格式化字符串时的小技巧

    java.text.MessageFormat格式化字符串时的小技巧 public static void main(String[] args) throws InterruptedExceptio ...

  2. java基础之——DecimalFormat格式化数字

    DecimalFormat可以用来格式化数字,例如用来设定保留多少位小数.设定数字分隔符等. 说方法之前,先介绍一下其常用的几个模式占位符: 0 一个数字# 一个数字,不包括 0. 小数的分隔符的占位 ...

  3. js格式化数字和金额

    格式化数字,格式化金额: function number_format(number, decimals, dec_point, thousands_sep) { /* * 参数说明: * numbe ...

  4. 在jsp页面中实现格式化数字,百分比,货币

    当时的要求是在jsp页面中计算百分比 实现方法 1.引入Jstl的fmt指令 <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" ...

  5. Smarty格式化数字为INT数

    <? require("setup.php"); define('PAGETITLE','pagtitle'); function insert_top($lid,$sid) ...

  6. php number_format()保留小数点后几位有效数的函数 千位分组来格式化数字(转)

    PHP保留小数点后2位的函数number_format number_format(带小数点的书,小数点后保留的位数) number_format(8.3486,2);  //取得小数点后2位有效数/ ...

  7. JSP页面格式化数字或时间 基于jstl的

    jsp页面格式化数字或时间 转载自: http://blog.csdn.net/hakunamatata2008/archive/2011/01/21/6156203.aspx Tags fmt:re ...

  8. php的格式化数字函数

    php格式化数字:位数不足前面加0补足 php格式化数字:位数不足前面加0补足 感谢:http://www.cnblogs.com/xiaochaohuashengmi/archive/2011/12 ...

  9. js 格式化数字,格式化金额:

    js 格式化数字,格式化金额: function number_format(number, decimals, dec_point, thousands_sep) { /* * 参数说明: * nu ...

随机推荐

  1. mysql hang and srv_error_monitor_thread using 100% cpu(已解决)

    昨天晚上,运维过来说有台生产服务器的mysql cpu一直100%,新的客户端登录不了,但是已经在运行的应用都正常可用. 登录服务器后,top -H看了下,其中一个线程的cpu 一直100%,其他的几 ...

  2. C++设计模式(第一周)

    part 1 设计模式简介 课程目标 1.理解松耦合设计思想 2.掌握面向对象设计原则 3.掌握重构技法改善设计 4.掌握GOF 核心设计模式 什么是设计模式? “每一个模式描述了一个在我们周围不断重 ...

  3. 20145322何志威 Exp8 Web基础

    20145322何志威 Exp8 Web基础 实践过程记录 一.Apache 1 修改/etc/apache2/ports.conf里的端口为5322后重新开启: 2 可以在浏览器中输入localho ...

  4. ["1", "2", "3"].map(parseInt) 为何返回[1,NaN,NaN]

    转载自:http://blog.csdn.net/freshlover/article/details/19034079 这涉及到是否深入理解两个函数的格式与参数含义. 首先根据我对两个函数用法的了解 ...

  5. Educational Codeforces Round 21 Problem F (Codeforces 808F) - 最小割 - 二分答案

    Digital collectible card games have become very popular recently. So Vova decided to try one of thes ...

  6. 【转】各种消息下wParam及lParam值的含义

    转载自:http://bbs.fishc.com/forum.php?mod=viewthread&tid=52668#lastpost 01.WM_PAINT消息 LOWORD(lParam ...

  7. Python3基础 __repr__ 实例对象的名字,可以显示信息

             Python : 3.7.0          OS : Ubuntu 18.04.1 LTS         IDE : PyCharm 2018.2.4       Conda ...

  8. Inversion of Control Containers and the Dependency Injection pattern

    https://martinfowler.com/articles/injection.html One of the entertaining things about the enterprise ...

  9. C#中的编译开关

    c#不支持宏定义,只支持编译开关. ======================================我想加上#define xxx就编译a代码,否则编译b代码,像这样的:#if xxxa ...

  10. springboot中配置主从redis

    测试redis的主从配置 redis实例 文件夹名称如下 redis_master_s redis_slaver1_s redis_slaver2_s redis.conf文件 master的redi ...