Java Data Type How to - Use String.format to convert a long to a String in currency format








Question

We would like to know how to use String.format to convert a long to a String in currency format.

Answer

import java.util.Locale;
/*  w  w w. j  av  a 2  s  . c o  m*/
public class Main {
  public static void main(String[] args) {
    long value = 123132;
    print(value);
  }

  public static void print(long value) {
    System.out
        .println(String.format(Locale.US, "$%,.2f", (double) value / 100));
  }
}