java格式化输出float_【译】Java8官方教程:格式化输出数值类型

格式化输出数值类型

前面介绍了如何使用print和println方法将字符串打印到标准输出(System.out)。由于所有数字都可以转换为字符串(您将在本课的后面看到),所以可以使用这些方法打印出字符串和数字的任意组合。但是,Java编程语言还有其他方法,可以在包含数字时对打印输出进行更多的控制。

printf和format方法

java.io包包含一个PrintStream类,它有两种格式化方法(format、printf),可以用来替换print和println,这两个方法作用等效。你使用过的System.out恰好是一个PrintStream对象,所以你可以通过System.out调用PrintStream的方法。因此,您可以在以前使用print或println的任意位置使用format或printf。

对于java.io.PrintStream的方法:

public PrintStream format(String format, Object... args)

复制代码

其中format是指定要使用的格式的字符串,args是使用该格式打印的变量列表。一个例子:

System.out.format("The value of " + "the float variable is " +

"%f, while the value of the " + "integer variable is %d, " +

"and the string is %s", floatVar, intVar, stringVar)

复制代码

第一个参数format是一个格式字符串,它指定如何格式化第二个参数args中的对象。格式字符串包含纯文本和格式说明符(格式化参数的特殊字符)。

Object... args称为可变长变量,表明参数的个数是不固定的

格式说明符以百分号(%)开始,以转换器结束。转换器是一个字符,指示要格式化的参数的类型。在百分号(%)和转换器之间可以有可选的标志和描述符。有许多转换器、标志和描述符,它们都在java.util.Formatter中有说明。

下面是一个基本的例子:

int i = 461012;

System.out.format("The value of i is: %d%n", i);

复制代码

%d指定十进制整数变量,%n是一个独立于平台的换行符,输出是:

The value of i is: 461012

复制代码

printf和format方法被重载。都有一个具有以下语法的版本:

public PrintStream format(Locale l, String format, Object... args)

复制代码

例如,要在法语系统中打印数字(在浮点数表示中使用逗号代替小数点),可以使用

System.out.format(Locale.FRANCE,

"The value of the float " + "variable is %f, while the " +

"value of the integer variable " + "is %d, and the string is %s%n",

floatVar, intVar, stringVar);

复制代码

下表列出了样例程序TestFormat.java中使用的一些转换器和标志

d7d539bfc6512ed68561b4b5323aea55.png

import java.util.Calendar;

import java.util.Locale;

public class TestFormat {

public static void main(String[] args) {

long n = 461012;

System.out.format("%d%n", n); // --> "461012"

System.out.format("%08d%n", n); // --> "00461012"

System.out.format("%+8d%n", n); // --> " +461012"

System.out.format("%,8d%n", n); // --> " 461,012"

System.out.format("%+,8d%n%n", n); // --> "+461,012"

double pi = Math.PI;

System.out.format("%f%n", pi); // --> "3.141593"

System.out.format("%.3f%n", pi); // --> "3.142"

System.out.format("%10.3f%n", pi); // --> " 3.142"

System.out.format("%-10.3f%n", pi); // --> "3.142"

System.out.format(Locale.FRANCE,

"%-10.4f%n%n", pi); // --> "3,1416"

Calendar c = Calendar.getInstance();

System.out.format("%tB %te, %tY%n", c, c, c); // --> "May 29, 2006"

System.out.format("%tl:%tM %tp%n", c, c, c); // --> "2:34 am"

System.out.format("%tD%n", c); // --> "05/29/06"

}

}

复制代码

DecimalFormat类

您可以使用java.text.DecimalFormat类来控制前置和后置零、前缀和后缀、分组(千)分隔符和小数分隔符的显示。DecimalFormat在数字格式化方面提供了很大的灵活性,但是它会使代码更加复杂

下面的例子中通过将格式字符串传递给DecimalFormat构造函数,创建了一个DecimalFormat对象myFormatter。format()方法是从NumberFormat继承而来,由myformatter调用——它接受double类型的值作为参数并返回格式化后的字符串。

译者注:这个类和BigDecimal没有半点关系 ,他们都不在一个包内

下面是一个示例程序,演示DecimalFormat的使用:

import java.text.*;

public class DecimalFormatDemo {

static public void customFormat(String pattern, double value ) {

DecimalFormat myFormatter = new DecimalFormat(pattern);

String output = myFormatter.format(value);

System.out.println(value + " " + pattern + " " + output);

}

static public void main(String[] args) {

customFormat("###,###.###", 123456.789);

customFormat("###.##", 123456.789);

customFormat("000000.000", 123.78);

customFormat("$###,###.###", 12345.67);

}

}

复制代码

输出为:

123456.789 ###,###.### 123,456.789

123456.789 ###.## 123456.79

123.78 000000.000 000123.780

12345.67 $###,###.### $12,345.67

复制代码

下表解释了每一行输出:

90626c949113531ffc689c52d0b3be30.png

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值