首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

DecimalFormat的用法引见

2012-06-27 
DecimalFormat的用法介绍?总结:要生成一个DecimalFormat对象,一般只要通过NumberFormat类工厂的getInstanc

DecimalFormat的用法介绍

?总结:

要生成一个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#;(#)”这个模板的意思是指数字的负数形式跟正数的一样。

?

热点排行