java.text.DecimalFormat学习笔记
2014-05-14 22:14
323 查看
例子:
import java.text.*;
public class Untitled1 {public static void main(String[] args) {
//---------------------------------------------
//定义一个数字格式化对象,格式化模板为".##",即保留2位小数.
DecimalFormat a = new DecimalFormat(".##");
String s= a.format(333.335);
System.err.println(s);
//说明:如果小数点后面不够2位小数,不会补零.参见Rounding小节
//---------------------------------------------
//-----------------------------------------------
//可以在运行时刻用函数applyPattern(String)修改格式模板
//保留2位小数,如果小数点后面不够2位小数会补零
a.applyPattern(".00");
s = a.format(333.3);
System.err.println(s);
//------------------------------------------------
//------------------------------------------------
//添加千分号
a.applyPattern(".##\u2030");
s = a.format(0.78934);
System.err.println(s);//添加千位符后,小数会进三位并加上千位符
//------------------------------------------------
//------------------------------------------------
//添加百分号
a.applyPattern("#.##%");
s = a.format(0.78645);
System.err.println(s);
//------------------------------------------------
//------------------------------------------------
//添加前、后修饰字符串,记得要用单引号括起来
a.applyPattern("'这是我的钱$',###.###'美圆'");
s = a.format(33333443.3333);
System.err.println(s);
//------------------------------------------------
//------------------------------------------------
//添加货币表示符号(不同的国家,添加的符号不一样
a.applyPattern("\u00A4");
s = a.format(34);
System.err.println(s);
//------------------------------------------------
//-----------------------------------------------
//定义正负数模板,记得要用分号隔开
a.applyPattern("0.0;'@'-#.0");
s = a.format(33);
System.err.println(s);
s = a.format(-33);
System.err.println(s);
//-----------------------------------------------
//综合运用,正负数的不同前后缀
String pattern="'my moneny'###,###.##'RMB';'ur money'###,###.##'US'";
a.applyPattern(pattern);
System.out.println(a.format(1223233.456));
}
}
总结:
要生成一个DecimalFormat对象,一般只要通过NumberFormat类工厂的getInstance()来取得一个 NumberFormat对象再将其转换成DecimalFormat对象,然后通过DecimalForat对象的applyPattern()来动态 改变数据的现示格式模板,通过format()方法取得格式化后的数字。同时,DecimalFormat提供了许多的方法来返回格式化后的数字的某一部 份,这些方法如:getNegativeSuffix()。这个类的难点主要是在模板的书写及理解上。其实主要的就是针对一个数字的正负形式来设定不同的
格式显示。这里要特别注意的是使用在模板上的特殊字符代表有特殊的意义,如下表所示:
Symbol Description
0 a digit
# a digit, zero shows as absent
. placeholder for decimal separator
, placeholder for grouping separator
E separates mantissa and exponent for exponential formats
; separates formats
- default negative prefix
% multiply by 100 and show as percentage
? multiply by 1000 and show as per mille
¤ currency sign; replaced by currency symbol; if doubled, replaced by international currency symbol; if present in a pattern, the monetary decimal separator is used instead of the decimal separator
X any other characters can be used in the prefix or suffix
' used to quote special characters in a prefix or suffix
例如:如果模板中含有#,意思是指这个#号可代表一个或多个数字如果该位的数字是零的话则省略该位。另:注意“#,##0.0#;(#)”这个模板的意思是指数字的负数形式跟正数的一样。
相关文章推荐
- JAVA学习笔记(四)substring、System.exit(1)、java.text.SimpleDateFormat、==、equals、StringBuffer、SQl动态查询
- 说说java.text.DecimalFormat的用法
- Android(java)学习笔记88:TextView属性大全
- java.text.DecimalFormat遇到5不进位问题
- java.text.DecimalFormat
- java.text.DecimalFormat()格式化输出数字
- java.text.DecimalFormat 格式化数据的显示效果:补全小数点后的位数、三位一个逗号分割、四舍五入
- java.text.DecimalFormat 工具类
- Android(java)学习笔记147:textView 添加超链接(两种实现方式,,区别于WebView)
- java.text.DecimalFormat类(十进制格式化)
- Java学习笔记——dateformat的应用
- Hadoop2.6.0学习笔记(四)TextInputFormat及RecordReader解析
- Java学习笔记(12)Exception Handling and Text I/O
- java.text.DecimalFormat学习笔记
- Java基础学习笔记十三 常用API之正则表达式、Date、DateFormat、Calendar
- java.lang.IllegalArgumentException: class java.text.DecimalFormat declares multiple JSON fields name
- Android(java)学习笔记139:在TextView组件中利用Html插入文字或图片
- java.text.DecimalFormat学习笔记
- java.text.DecimalFormat
- 我的Thinking in Java学习笔记(十)