Java中格式化输出数字

https://blog.csdn.net/chuyouyinghe/article/details/51219436

#0.例子-丈母娘要钱,第一天1分,以后每天double

public static void getMoney(int days){
		DecimalFormat df1=(DecimalFormat) DecimalFormat.getInstance();
		df1.setGroupingSize(4);
		long dayMoney = 1;
		long totalMoney = 1;
		if(days==1){
			System.out.println("现在是第"+days+"天,
			我总共有"+df1.format(totalMoney/100.0)+"元");
		}
		for(int i=2;i<=days;i++){
			dayMoney = 2*dayMoney;
			totalMoney = totalMoney+dayMoney;
			System.out.println("现在是第"+i+"天,
			我总共有"+df1.format(totalMoney/100.0)+"元");
		}
	}

结果:
现在是第2天,我有0.02元,我总共有0.03元
现在是第3天,我有0.04元,我总共有0.07元
现在是第4天,我有0.08元,我总共有0.15元
现在是第5天,我有0.16元,我总共有0.31元
现在是第6天,我有0.32元,我总共有0.63元
现在是第7天,我有0.64元,我总共有1.27元
现在是第8天,我有1.28元,我总共有2.55元
现在是第9天,我有2.56元,我总共有5.11元
现在是第10天,我有5.12元,我总共有10.23元
现在是第11天,我有10.24元,我总共有20.47元
现在是第12天,我有20.48元,我总共有40.95元
现在是第13天,我有40.96元,我总共有81.91元
现在是第14天,我有81.92元,我总共有163.83元
现在是第15天,我有163.84元,我总共有327.67元
现在是第16天,我有327.68元,我总共有655.35元
现在是第17天,我有655.36元,我总共有1310.71元
现在是第18天,我有1310.72元,我总共有2621.43元
现在是第19天,我有2621.44元,我总共有5242.87元
现在是第20天,我有5242.88元,我总共有1,0485.75元
现在是第21天,我有1,0485.76元,我总共有2,0971.51元
现在是第22天,我有2,0971.52元,我总共有4,1943.03元
现在是第23天,我有4,1943.04元,我总共有8,3886.07元
现在是第24天,我有8,3886.08元,我总共有16,7772.15元
现在是第25天,我有16,7772.16元,我总共有33,5544.31元
现在是第26天,我有33,5544.32元,我总共有67,1088.63元
现在是第27天,我有67,1088.64元,我总共有134,2177.27元
现在是第28天,我有134,2177.28元,我总共有268,4354.55元
现在是第29天,我有268,4354.56元,我总共有536,8709.11元
现在是第30天,我有536,8709.12元,我总共有1073,7418.23元

#1.原文如下
今天用到了,在网上查到这篇文章不错,呵呵!记录下。

在实际工作中,常常需要设定数字的输出格式,如以百分比的形式输出,或者设定小数位数等,现稍微总结如下。
主要使用的类:java.text.DecimalFormat
1。实例化对象,可以用如下两种方法:
DecimalFormat df=(DecimalFormat)NumberFormat.getInstance();
DecimalFormat df1=(DecimalFormat) DecimalFormat.getInstance();
因为DecimalFormat继承自NumberFormat。
2。设定小数位数
系统默认小数位数为3,如:
DecimalFormat df=(DecimalFormat)NumberFormat.getInstance();
System.out.println(df.format(12.3456789));
输出:12.346
现在可以通过如下方法把小数为设为两位:
df.setMaximumFractionDigits(2);
System.out.println(df.format(12.3456789));
则输出为:12.35
3。将数字转化为百分比输出,有如下两种方法:
(1)
df.applyPattern("##.##%");
System.out.println(df.format(12.3456789));
System.out.println(df.format(1));
System.out.println(df.format(0.015));
输出分别为:1234.57% 100% 1.5%
(2)
df.setMaximumFractionDigits(2);
System.out.println(df.format(12.3456789100)+"%");
System.out.println(df.format(1
100)+"%");
System.out.println(df.format(0.015*100)+"%");
输出分别为:
1,234.57% 100% 1.5%
4。设置分组大小
DecimalFormat df1=(DecimalFormat) DecimalFormat.getInstance();
df1.setGroupingSize(2);
System.out.println(df1.format(123456789));
输出:1,23,45,67,89
还可以通过df1.setGroupingUsed(false);来禁用分组设置,如:
DecimalFormat df1=(DecimalFormat) DecimalFormat.getInstance();
df1.setGroupingSize(2);
df1.setGroupingUsed(false);
System.out.println(df1.format(123456789));
输出:123456789
5。设置小数为必须为2位
DecimalFormat df2=(DecimalFormat) DecimalFormat.getInstance();
df2.applyPattern(“0.00”);
System.out.println(df2.format(1.2));
输出:1.20

java.text.DecimalFormat学习笔记

例子:
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, themonetary 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#?#)”这个模板的意思是指数字的负数形式跟正数的一样。

转自:http://www.cnblogs.com/zhxiaomiao/archive/2009/09/01/1558125.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值