简体   繁体   中英

Java Converting long into currency

I am having trouble converting a long (cents) into currency format.

My Code:

long doublePayment = 1099;  //Should equal $10.99
DecimalFormat dFormat = new DecimalFormat();
String formattedString = dFormat.format(doublePayment);
System.out.println(formattedString);

Output: 1,099

I also tried:

long doublePayment = 1099;
NumberFormat n = NumberFormat.getCurrencyInstance(Locale.US); 
String s = n.format(doublePayment);
System.out.println(s);

Since this is cents, the output should be 10.99 or $10.99.

Cant figure out what I am doing wrong. Thanks!!!

In case You have long to start with, you still should use java.math.BigDecimal.

    long doublePayment = 1099;
    BigDecimal payment = new BigDecimal(doublePayment).movePointLeft(2);
    System.out.println("$" + payment); // produces: $10.99

Let it be once again said out loud: One should never use floating-point variables to store money/currency value.

To convert cents to dollars you can use

long doublePayment = 1099;
NumberFormat n = NumberFormat.getCurrencyInstance(Locale.US); 
String s = n.format(doublePayment / 100.0);
System.out.println(s);

This will be accurate up to $70 trillion.

Your literal is 1099 , which is a thousand and ninety nine , coping with the Java rules for integer literals. According to your JVM locale, this number is represented with 1,099 . If you were in Europe, it'd be 1.099 . So, it's not an issue with your output , but with your input .

The problem is that you have to represent a fixed point value, but you don't know java.math.BigDecimal and try to fake it. Things will broken when you'll do some computations. Don't do it.

This is what you are supposed to do. Simply (it's far less code, too):

BigDecimal payment = new BigDecimal("10.99");
System.out.println(String.format("$%.2f", payment));

Note how you really initailize a number with a String . Also, String.format() will take care of the current locale , or you could supply the required one via the overloaded method.

None of these answers take into account that currencies in different locales have different numbers of fractional digits. For example, the Japanese Yen has zero fractional digits while the Jordanian Dinar has three, and then of course, the US Dollar has two. I believe this is a more appropriate solution:

    long payment = 1099L;
    BigDecimal bd = new BigDecimal(payment);

    Locale jpLocale = new Locale("ja", "JP");//Japan

    Currency jpCurrency = Currency.getInstance(jpLocale);
    NumberFormat jpnf = NumberFormat.getCurrencyInstance(jpLocale);
    int jpNumFractionalDigits = jpCurrency.getDefaultFractionDigits();

    BigDecimal jpbd = bd.movePointLeft(jpNumFractionalDigits);


    Locale usLocale = new Locale("en", "US");//United States

    Currency usCurrency = Currency.getInstance(usLocale);
    NumberFormat usnf = NumberFormat.getCurrencyInstance(usLocale);
    int usNumFractionalDigits = usCurrency.getDefaultFractionDigits();

    BigDecimal usbd = bd.movePointLeft(usNumFractionalDigits);



    System.out.println(jpnf.format(jpbd)); //prints ¥1,099
    System.out.println(usnf.format(usbd));//prints $10.99

double doublePayment = 10.99; You need to provide currency like this. NumberFormat will not understand whether you are providing it in cents or $ unless it see decimal point

It's not an issue with formatting, it's an issue with input. Divide your input by 100 and you'll be all set.

float payment = 1099 / ((float) 100);

Use currency formatter:

NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.ENGLISH);
String output = nf.format(value);
System.out.println(value + " " + output);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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