【问题标题】:How can I format a double to something using comma and euro symbol如何使用逗号和欧元符号将双精度格式格式化
【发布时间】:2017-08-16 22:23:49
【问题描述】:

我有一个变量:

private double classicpreis  = 2.5;

我想把它改成这样:

private double classicpreis  = 2,5 €;

我认为逗号可以与 double 一起使用,但它不起作用。那么我该如何实现呢?

编辑

我想把它输出成这样的:

2,50 €

【问题讨论】:

  • 小数点与逗号是一个演示问题。代码仍然需要使用小数点。但是当你想输出值时,你可以随意格式化它。
  • 在代码本身中,还是作为字符串?对于代码本身,您将无法在其中使用逗号或货币符号作为值的一部分。
  • 如果您确实需要逗号,请改用String
  • 你不能改变语言本身,在代码里面你总是需要写小数点。然而,有多种方法可以用 comma 而不是 point 打印值。因此,您应该始终使用当前的语言环境格式,例如,如果用户在德国,那么它应该打印 comma。 Java 库中对此有自动化处理,如下所示:How to format double value for a given locale and number of decimal places?
  • 它只用于输出。在代码本身中,它可以是点。我只需要找出如何以我猜的正确方式格式化它。

标签: java number-formatting


【解决方案1】:

最简单的解决方案是使用 NumberFormat.getCurrencyInstance(Locale)NumberFormat.getCurrenyInstance() 并让它完成所有的格式化。

假设你有

double preis = 2.5;

然后你可以例如做

Locale locale = Locale.GERMANY;
NumberFormat numberFormat = NumberFormat.getCurrencyInstance(locale);
String s = numberFormat.format(preis);

你会得到"2,50 €"

请注意,格式会考虑所有细节(使用小数 逗号或点,选择的货币符号,数字之前或之后的货币, 之间的空格数)取决于您使用的Locale。 例子:对于Locale.GERMANY,你得到"2,50 €", 对于Locale.US,你得到"$2.50",对于Locale,UK,你得到"£2.50"

【讨论】:

  • 非常感谢您对我的帮助很大。我的代码现在运行良好。
【解决方案2】:

您可以将代码中的数字写为字符串常量,并使用您选择的语言环境将其解析为双精度数。但是那个欧元符号让我相信你根本不应该使用double,而应该使用十进制类。

【讨论】:

    【解决方案3】:

    您无法更改语言本身,如果您希望声明double 值,您将始终需要在代码中写入小数点。


    但是,有多种方法可以使用逗号而不是点来打印值,例如在应用程序用户可以看到的显示中。

    因此,您应该始终使用当前的区域设置格式,例如,如果用户在德国,那么它应该打印 comma。 Java 库中对此有自动化处理,如下所示:How to format double value for a given locale and number of decimal places?official tutorial by Oracle

    要使用的类是NumberFormatDecimalFormat,这里是一个小例子:

    Locale currentLocale = ...
    NumberFormat numberFormatter = NumberFormat.getNumberInstance(currentLocale);
    
    double value = 2.5;
    
    String formattedValue = numberFormatter.format(value);
    System.out.println("Formatted value is: " + value);
    

    现在输出会根据您为currentLocale 设置的内容而变化。您当前的语言环境可以通过Locale.getDefault() 获取,但您也可以直接从不同区域选择语言环境,例如使用Locale 中定义的常量,如Locale.GERMANY


    您还可以应用十进制模式来创建像1.004,34 这样的数字。因此模式是#,##0.00,可以这样使用:

    String pattern = "#,##0.00";
    DecimalFormat decimalFormatter = (DecimalFormat) numberFormatter; // The one from above
    
    decimalFormatter.applyPattern(pattern);
    
    String formattedValue = decimalFormatter.format(value);
    

    使用格式模式,您还可以添加 符号,只需将其添加到模式中即可。

    【讨论】:

    • 非常感谢您对我的帮助很大。我的代码现在运行良好。
    【解决方案4】:

    它现在运行良好。我的工作代码如下所示:

    public class Bestellterminal { 
    
    private double Preis = 0;
    private double classicpreis  = 2.50;
    
    classic.addActionListener(new ActionListener() {
    
            public void actionPerformed(ActionEvent e) {
    
                Preis = Preis + classicpreis;       
    
                Locale currentlocale = Locale.GERMANY;
                NumberFormat numberFormatter = 
                NumberFormat.getCurrencyInstance(currentlocale);
                String classicpreisx = numberFormatter.format(classicpreis);
                String preisx = numberFormatter.format(Preis);
    
    
    
                JLabel.setText(String.valueOf("Summe: " + preisx));
    
                }});
    
    }
    

    感谢大家的出色帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-07
      • 1970-01-01
      • 1970-01-01
      • 2023-03-25
      • 2020-09-25
      • 2012-11-22
      • 1970-01-01
      • 2022-12-06
      相关资源
      最近更新 更多