如何使用逗号和欧元符号将双精度格式化为某些东西
How can I format a double to something using comma and euro symbol
我有一个变量:
private double classicpreis = 2.5;
我想把它改成这样:
private double classicpreis = 2,5 €;
我认为逗号可以用 double,但它不起作用。那么我该如何实现呢?
编辑
我想输出成这样:
2,50 €
您可以在代码中将数字写为字符串常量,然后使用您选择的语言环境将其解析为双精度数。但是那个欧元符号让我相信你根本不应该使用 double
,而是小数 class.
您不能更改语言本身,如果您希望声明 double
值,在代码中您将始终需要写小数点。
然而,有多种方法可以使用逗号而不是点打印值,例如在应用程序用户看到的显示中。
因此,您应该始终使用当前的 区域设置格式 ,例如,如果用户在德国,那么它应该打印 逗号 。 Java 库中有针对此的自动化流程,如下所示:How to format double value for a given locale and number of decimal places? or in an official tutorial by Oracle.
要使用的class是NumberFormat
或DecimalFormat
,这里是一个小例子:
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);
使用格式模式,您还可以添加 €
符号,只需将其添加到模式即可。
最简单的解决方案是使用
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
得到 ".50"
,Locale,UK
得到 "£2.50"
.
现在 运行 很好。我的工作代码如下所示:
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));
}});
}
感谢大家的大力帮助。
我有一个变量:
private double classicpreis = 2.5;
我想把它改成这样:
private double classicpreis = 2,5 €;
我认为逗号可以用 double,但它不起作用。那么我该如何实现呢?
编辑
我想输出成这样:
2,50 €
您可以在代码中将数字写为字符串常量,然后使用您选择的语言环境将其解析为双精度数。但是那个欧元符号让我相信你根本不应该使用 double
,而是小数 class.
您不能更改语言本身,如果您希望声明 double
值,在代码中您将始终需要写小数点。
然而,有多种方法可以使用逗号而不是点打印值,例如在应用程序用户看到的显示中。
因此,您应该始终使用当前的 区域设置格式 ,例如,如果用户在德国,那么它应该打印 逗号 。 Java 库中有针对此的自动化流程,如下所示:How to format double value for a given locale and number of decimal places? or in an official tutorial by Oracle.
要使用的class是NumberFormat
或DecimalFormat
,这里是一个小例子:
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);
使用格式模式,您还可以添加 €
符号,只需将其添加到模式即可。
最简单的解决方案是使用 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
得到 ".50"
,Locale,UK
得到 "£2.50"
.
现在 运行 很好。我的工作代码如下所示:
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));
}});
}
感谢大家的大力帮助。