Java中使用String.format()格式化double类型数据

212

如何使用String.format(format, args)将浮点数格式化为如下格式?

2354548.235 -> 2,354,548.23

7个回答

335
String.format("%1$,.2f", myDouble);

String.format 自动使用默认语言环境。


39
您可以使用特定的语言环境,方法是使用 String.format(Locale.GERMAN, "%1$,.2f", myDouble);。该方法会将 myDouble 格式化为德语环境下保留两位小数的字符串。 - Olivier Grégoire
13
如果有多个双精度数需要格式化,可以使用 %1、%2 等。例如 String.format("%1$,.2f, %2$,.2f", myDouble, aDouble)。 - Moesio
5
我认为在这种情况下"%1$"是可选的。只使用"%,.2f"作为格式字符串对我来说可以正常工作。 - Matt Passell
5
是的,Matt是正确的。%1, %2等可以根据输入参数的索引重新排序输出。请参见此文档。您可以省略索引,格式化程序将采用默认顺序。 - praneetloke
2
你应该解释不同部分的含义。 - Mitulát báti

50
String.format("%4.3f" , x) ;

这意味着我们需要在结果中拥有4个数字,其中3个应该在小数点后面。

f是double的格式说明符。x代表我们要找到它的变量。

对我有用......


1
我认为这意味着它将是随后的参数中的第四个变量,并且有3个小数位。 - Michael Fulton
@MichaelFulton 这并不容易:String.format("%014.3f" , 58.656565) -> 0000000058,657:总共14个符号,其中有8个零和5个数字以及1个逗号。因此,在这种情况下,数字14表示结果字符串中的符号总数(多余的符号将被前导零替换,可以省略,然后将替换为空格)。.3部分在这里完全独立。 - parsecer
然而在这里:String.format("%014.3f", 58.656565) -> 58,657。我理解是因为优先级在.3f部分:首先打印带有3位小数的数字,然后如果还有剩余符号,则打印前导空格(或零/其他)。在这种情况下,数字是2 + 3 + 1 = 6个符号,我们只想打印4个,所以肯定没有多余的空格。 - parsecer
@parsecer,你两次发布了相同的 String.format("%014.3f" , 58.656565),但结果不同,我有什么遗漏吗? - RAM237
@RAM237 是的,肯定是打错了。但现在我不记得具体是哪里出了问题了。 - parsecer

27

如果你希望手动设置符号格式,可以使用以下方法:

DecimalFormatSymbols decimalFormatSymbols = new DecimalFormatSymbols();
decimalFormatSymbols.setDecimalSeparator('.');
decimalFormatSymbols.setGroupingSeparator(',');
DecimalFormat decimalFormat = new DecimalFormat("#,##0.00", decimalFormatSymbols);
System.out.println(decimalFormat.format(1237516.2548)); //1,237,516.25

但是,首选基于区域设置的格式化。


4
有用的回答!然而,最后一句话“Locale-based formatting is preferred, though”没有上下文是没有意义的。有很好的使用情况,你不应该使用基于地区的格式化,比如当你想要生成特定格式的字符串时。例如,你正在实现将数据导出到外部文件,并且希望完全控制格式,不依赖于当前(或任何)语言环境。 - Honza Zidek

22

该代码来自此链接 ;

Double amount = new Double(345987.246);
NumberFormat numberFormatter;
String amountOut;

numberFormatter = NumberFormat.getNumberInstance(currentLocale);
amountOut = numberFormatter.format(amount);
System.out.println(amountOut + " " + 
                   currentLocale.toString());

这个示例的输出显示了同一个数字在不同地区设置下的格式变化:

345 987,246  fr_FR
345.987,246  de_DE
345,987.246  en_US

13
public class MainClass {
   public static void main(String args[]) {
    System.out.printf("%d %(d %+d %05d\n", 3, -3, 3, 3);

    System.out.printf("Default floating-point format: %f\n", 1234567.123);
    System.out.printf("Floating-point with commas: %,f\n", 1234567.123);
    System.out.printf("Negative floating-point default: %,f\n", -1234567.123);
    System.out.printf("Negative floating-point option: %,(f\n", -1234567.123);

    System.out.printf("Line-up positive and negative values:\n");
    System.out.printf("% ,.2f\n% ,.2f\n", 1234567.123, -1234567.123);
  }
}

并打印出:

3 (3) +3 00003
默认浮点数格式:1234567,123000
带逗号的浮点数格式:1.234.567,123000
负浮点数默认格式:-1.234.567,123000
负浮点数选项格式:(1.234.567,123000)

排列正负值:
1.234.567,12
-1.234.567,12


2

有许多方法可以做到这一点。以下是其中之一:

假设您的原始数字如下: double number = 2354548.235;

使用 NumberFormat 和舍入模式

最初的回答:

NumberFormat nf = DecimalFormat.getInstance(Locale.ENGLISH);
    DecimalFormat decimalFormatter = (DecimalFormat) nf;
    decimalFormatter.applyPattern("#,###,###.##");
    decimalFormatter.setRoundingMode(RoundingMode.CEILING);

    String fString = decimalFormatter.format(number);
    System.out.println(fString);

使用字符串格式化器

System.out.println(String.format("%1$,.2f", number));

在所有情况下,输出将为:2354548.24 注意
在四舍五入时,您可以在格式化程序中添加 RoundingMode。以下是一些舍入模式
    decimalFormat.setRoundingMode(RoundingMode.CEILING);
    decimalFormat.setRoundingMode(RoundingMode.FLOOR);
    decimalFormat.setRoundingMode(RoundingMode.HALF_DOWN);
    decimalFormat.setRoundingMode(RoundingMode.HALF_UP);
    decimalFormat.setRoundingMode(RoundingMode.UP);

以下是导入内容:

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Locale;

1
使用 DecimalFormat
 NumberFormat nf = DecimalFormat.getInstance(Locale.ENGLISH);
 DecimalFormat decimalFormatter = (DecimalFormat) nf;
 decimalFormatter.applyPattern("#,###,###.##");
 String fString = decimalFormatter.format(myDouble);
 System.out.println(fString);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接