Java Converting long into currency

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.


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.


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

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.