如何在数字前附加数字符号

时间:2016-06-01 14:21:05

标签: java

我正在用Java编写一个程序,要求用户提供员工姓名和工资。一旦输入并显示在屏幕上,我需要在工资前面加上一个数字符号。知道我会怎么做吗?

提前致谢!

    public static void main(String[] args) {

        //use default constructor
        //ask name
        String name = JOptionPane.showInputDialog("What is Employee #1's name?");
        //System.out.println(name);
        //ask salary
        double salary = Double.parseDouble(JOptionPane.showInputDialog("What is the employee's salary?"));
        //System.out.print(salary);
        //make an employee1 value
        Employee employee1 = new Employee(name, salary);
        //print out
        System.out.println(employee1);
        System.out.printf("Employee: %s%n", employee1.getName());
        System.out.printf("Salary: %.3f%n", employee1.getSalary());
//      System.out.printf("Employee: %s", employee1.getName());
        JOptionPane.showMessageDialog(null, "This is the Employee's name:" + employee1.getName());
        JOptionPane.showMessageDialog(null, "This is the Employee's salary:" + employee1.getSalary());
        System.out.printf("%.3f", salary);


        //employee 2
        String name1 = JOptionPane.showInputDialog("What is Employee #2's name?");
        double salary1 = Double.parseDouble(JOptionPane.showInputDialog("What is the employee's salary?"));
        //System.out.print(salary);
        //make an employee2 value
        Employee employee2 = new Employee(name1, salary1);
        //print out
        System.out.println(employee2);
        System.out.printf("Employee: %s%n", employee2.getName());
        System.out.printf("Salary: %.3f%n", employee2.getSalary());
//      System.out.printf("Employee: %s", employee2.getName());
        JOptionPane.showMessageDialog(null, "This is the Employee's name:" + employee2.getName());
        JOptionPane.showMessageDialog(null, "This is the Employee's salary:" + employee2.getSalary());
        System.out.printf("%.3f", salary);


    }
}

4 个答案:

答案 0 :(得分:1)

如果您需要特定于区域设置的货币,请查看https://docs.oracle.com/javase/tutorial/i18n/format/numberFormat.html

代码例外:

static public void displayCurrency( Locale currentLocale) {

    Double currencyAmount = new Double(9876543.21);
    Currency currentCurrency = Currency.getInstance(currentLocale);
    NumberFormat currencyFormatter = 
        NumberFormat.getCurrencyInstance(currentLocale);

    System.out.println(
        currentLocale.getDisplayName() + ", " +
        currentCurrency.getDisplayName() + ": " +
        currencyFormatter.format(currencyAmount));
}

前面的代码行生成的输出如下:

French (France), Euro: 9 876 543,21 €
German (Germany), Euro: 9.876.543,21 €
English (United States), US Dollar: $9,876,543.21

答案 1 :(得分:0)

虽然不是最有活力的答案,但你真的可以在打印工资之前添加任何字符:

JOptionPane.showMessageDialog(null, "This is the Employee's salary: #" + employee2.getSalary());

对于大多数情况,这可能会很好。或者,如果不是数字符号,而是指美元符号,您可以在输出语句中替换它。

答案 2 :(得分:0)

https://docs.oracle.com/javase/7/docs/api/java/util/Formatter.html

  

'+' - - y4 y - 结果将始终包含一个符号

您可以将数字格式设置为始终包含符号

public static void main(String[] args)
{
    double s = 47.11;

    System.out.printf("%+.2f%n", s);

    System.out.println(String.format("%+.2f%n", -s));
}

<强>输出

+47,11
-47,11

答案 3 :(得分:0)

只需在主函数的开头添加以下行。

String numberSign = "#"; // Whatever you desire

然后在需要的地方使用它连接(它是String的'+')。

e.g。

JOptionPane.showMessageDialog(null, "This is the Employee's salary:" + numberSign + " " + employee2.getSalary());