简体   繁体   English

如何使用逗号和欧元符号将双精度格式设置为某些格式

[英]How can I format a double to something using comma and euro symbol

I have a variable: 我有一个变量:

private double classicpreis  = 2.5;

and I want to change it into something like this: 我想将其更改为以下内容:

private double classicpreis  = 2,5 €;

I thought a comma would be possible with double but it didn't work. 我以为双逗号可能会出现逗号,但没有用。 So how can I achieve that? 那么我该如何实现呢?

Edit 编辑

I want to output it to something like this: 我想将其输出为以下内容:

2,50 €

You can write the number in your code as a string constant, and parse it to a double using the locale of your choice. 您可以在代码中将数字写为字符串常量,然后使用您选择的语言环境将其解析为双精度型。 But that Euro sign leads me to believe that you should not be using double at all, but rather a decimal class. 但是那个欧元符号使我相信您根本不应该使用double ,而应该使用十进制。

You can not change the language itself, inside the code you will always need to write the decimal point if you wish to declare double values. 您不能更改语言本身,如果要声明double值,则在代码内部始终需要写小数点。


However there are multiple ways to print the value with comma instead of point, for example in the display the user of the application gets to see. 但是,有多种方法可以用逗号而不是点来打印值,例如,在应用程序的用户看到的显示中。

Therefore you should always use the current locale format , so for example if the user is in Germany then it should print comma . 因此,您应该始终使用当前的语言环境格式 ,例如,如果用户在德国,则应打印逗号 There are automated processes for this in the Java library, like seen here: How to format double value for a given locale and number of decimal places? Java库中对此有自动化的过程,如此处所示: 如何格式化给定语言环境的双精度值和小数位数? or in an official tutorial by Oracle . 或在Oracle官方教程中

The class to use is NumberFormat or DecimalFormat , here is a small example: 要使用的类是NumberFormatDecimalFormat ,这是一个小示例:

Locale currentLocale = ...
NumberFormat numberFormatter = NumberFormat.getNumberInstance(currentLocale);

double value = 2.5;

String formattedValue = numberFormatter.format(value);
System.out.println("Formatted value is: " + value);

The output now changes depending on what you set for the currentLocale . 现在,输出会根据您为currentLocale设置的内容而改变。 Your current locale can be get with Locale.getDefault() but you can also directly choose locales from different regions, for example with the constants defined in Locale like Locale.GERMANY . 您当前的语言环境可以通过Locale.getDefault()获得,但是您也可以直接从不同区域中选择语言环境,例如,使用Locale定义的常量,例如Locale.GERMANY


You can also apply decimal patterns to create numbers like 1.004,34 . 您还可以应用十进制模式来创建数字,例如1.004,34 The pattern therefore is #,##0.00 and it can be used this way: 因此#,##0.00模式为#,##0.00 ,可以通过以下方式使用:

String pattern = "#,##0.00";
DecimalFormat decimalFormatter = (DecimalFormat) numberFormatter; // The one from above

decimalFormatter.applyPattern(pattern);

String formattedValue = decimalFormatter.format(value);

With the format pattern you can also add the symbol, just add it to the pattern. 使用格式模式,您还可以添加符号,只需将其添加到模式中即可。

The most simple solution is to use NumberFormat.getCurrencyInstance(Locale) or NumberFormat.getCurrenyInstance() and let it do all the formatting. 最简单的解决方案是使用NumberFormat.getCurrencyInstance(Locale)NumberFormat.getCurrenyInstance()并让其进行所有格式设置。

Suppose you have 假设你有

double preis = 2.5;

then you can for example do 那么你可以例如

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

and you will get "2,50 €" . 然后您将获得"2,50 €"

Note that the formatting takes care for all the details (use of decimal comma or point, chosen currency symbol, currency before or after number, number of spaces between) depending on the Locale you use. 请注意,格式取决于所有细节(使用小数逗号或点,选择的货币符号,数字前后的货币,之间的空格数),具体取决于您使用的Locale Examples: For Locale.GERMANY you get "2,50 €" , for Locale.US you get "$2.50" , for Locale,UK you get "£2.50" . 示例:对于Locale.GERMANY您将获得"2,50 €" ;对于Locale.US您将获得"$2.50" ;对于Locale,UK您将获得"£2.50"

Its running fine now. 现在运行良好。 My working code looks like this: 我的工作代码如下所示:

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));

            }});

}

Thanks guys for your awesome help. 谢谢大家的大力帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM