-2

Hi here i have the code below to show a number into usd format

 Long amount = 1234L;
 NumberFormat formatter = NumberFormat.getCurrencyInstance(new Locale("en", "US"));
 String moneyString = formatter.format((double) amount / 1000);
 System.out.println(moneyString);

I am getting output as $1.23, but i need to show 4 also like $1.234,how to do this

I want my point to 3 decimal and in usd format.

let i am giving a number 150078 i will get o/p as 150.078 please help me.

Akshay Soam
  • 1,580
  • 3
  • 21
  • 39
Sushreesmita
  • 19
  • 1
  • 4

4 Answers4

1

Add this line after you create your formatter:

        formatter.setMinimumFractionDigits(3);
RealSkeptic
  • 33,993
  • 7
  • 53
  • 79
1

You could use DecimalFormat, check the link for further information on formatting patterns.

DecimalFormat decimalFormat = new DecimalFormat("$ #,##0.000");

System.out.println(decimalFormat.format(3.114));
System.out.println(decimalFormat.format(3.1146));
System.out.println(decimalFormat.format(123213143.1146));

It produces the following output:

$ 3.114
$ 3.115
$ 123,213,143.115

Without the need for manual rounding in your code.

PeterK
  • 1,697
  • 10
  • 20
0
use DecimalFormat

new DecimalFormat("#.####").format(someNumber)

see http://rextester.com/NVUE17479

Alex Pacurar
  • 5,801
  • 4
  • 26
  • 33
0

please have a look to this tutorial from Oracle: http://docs.oracle.com/javase/tutorial/i18n/format/decimalFormat.html

And the following code

        //Locale
    DecimalFormatSymbols dfs = new DecimalFormatSymbols(Locale.US);


    dfs.setDecimalSeparator('.');
    dfs.setGroupingSeparator(',');
    DecimalFormat df = new DecimalFormat("###,###,###,###.000", dfs);

    return df.format(amount);
Uluaiv
  • 188
  • 2
  • 8