开始学java之格式化数字输出

本文介绍了一个Java程序示例,展示了如何使用System.out.format进行数字和日期的不同格式化输出,包括数字的多种格式展示及利用Calendar类获取并显示当前日期。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import java.util.Calendar;
import java.util.Locale;
public class hello {
	public static void main(String[] args)
	{
		long n= 382112;
		System.out.format("%d%n",n);
		System.out.format("%08d%n", n);
		System.out.format("%+8d%n", n);
		System.out.format("%,8d%n", n);
		System.out.format("%+,8d%n", n);
double pi=Math.PI;//获取圆周率
System.out.format("%f%n", pi);
System.out.format("%.3f%n", pi);
System.out.format("%10.3f%n", pi);
System.out.format("%-10.3f%n", pi);
System.out.format(Locale.FRANCE,"%-10.4f%n", pi);//用法语表示
Calendar c=Calendar.getInstance();//获取表示当前日期的对象
System.out.format("%tB %te,%tY%n",c,c,c);
System.out.format("%tm %te,%tY%n",c,c,c);
System.out.format("%tl:%tM %tp%n",c,c,c);
System.out.format("%tD%n",c);

		
	}

}

转载于:https://www.cnblogs.com/huzhongzhong/archive/2011/07/08/2101392.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值