0

I have an amount

Long amount=83000;

I need to format it to $83.000, how to do this ?

NumberFormat formatter = NumberFormat.getCurrencyInstance(new Locale("en", "US"));

String moneyString = formatter.format(amount);

System.out.println(moneyString);

I am getting $83000.00 but i want my point before 3 last digit

Developer
  • 144
  • 11
Sushreesmita
  • 19
  • 1
  • 4
  • 1
    no i want $83.000,if amount is 67890 then output will be $67.890 please help if any idea – Sushreesmita Oct 13 '14 at 10:43
  • 3
    http://stackoverflow.com/questions/5323502/how-to-set-thousands-separator-in-java look like duplicate – Kannan Thangadurai Oct 13 '14 at 10:44
  • 1
    By *$83.000* you mean 83 dollars or 83 *thousands* dollars? – agad Oct 13 '14 at 12:39
  • in us `.` is decimal separator and `,` is thousand separator, in majority of european countries is other way around. so if you really want to display your number in europen format, try to use `NumberFormat formatter = NumberFormat.getNumberInstance(Locale.GERMANY);` and then append your money string after you format your number – user902383 Oct 13 '14 at 12:43

6 Answers6

3

This may help you,

 DecimalFormat myFormatter = new DecimalFormat("$###,###.###");
 String output = myFormatter.format(amount);
 System.out.println(output);
akash
  • 22,664
  • 11
  • 59
  • 87
Vishvesh Phadnis
  • 2,448
  • 5
  • 19
  • 35
0

You are using the wrong class. NumberFormatter will give Locale specific formatting. From the API docs:

Your code can be completely independent of the locale conventions for decimal points, thousands-separators, or even the particular decimal digits used, or whether the number format is even decimal.

You should rather look into DecimalFormat if you intend to output the same string regardless of the operating system's current locale.

By creating a formatting string such as "$#,##0" and passing an instance of DecimalFormatSymbols where you set the grouping character to '.' you will achieve your intended output without hacks.

oligofren
  • 20,744
  • 16
  • 93
  • 180
  • @ToyOno has an answer below that examplifies this. – oligofren Oct 13 '14 at 11:06
  • But for this number 8389000 its showing o/p 8.389.000,is there any way of removing extra dot after 8?? – Sushreesmita Oct 13 '14 at 11:21
  • That is an additional requirement not present in the original question. It seems _very_ custom, and probably not supported by DecimalFormat, so you might as well just use a custom solution to it as well: Just remove the extra dot from the string manually. – oligofren Oct 13 '14 at 11:42
0

How about this?

long num = 83000;
DecimalFormat df = new DecimalFormat("##,###");
String toReplace = df.format(83000).toString();
System.out.println(toReplace.replace(",", "."));
Adz
  • 2,809
  • 10
  • 43
  • 61
-1

This should work :

DecimalFormatSymbols symbols = new DecimalFormatSymbols(new Locale("en", "US"));
symbols.setDecimalSeparator(',');
symbols.setGroupingSeparator('.'); 
DecimalFormat df = new DecimalFormat("$#,##0", symbols);
System.out.println(df.format(83000L));
//$83.000
ToYonos
  • 16,469
  • 2
  • 54
  • 70
-2

Perhaps you meant this?

public static void main(String[] args) {
    Long amount = 67890L;
    NumberFormat formatter = NumberFormat.getCurrencyInstance(new Locale("en", "US"));
    String moneyString = formatter.format((double) amount / 1000);
    System.out.println(moneyString);
}

This code will print:

$67.89

shlomi33
  • 1,458
  • 8
  • 9
  • 1
    ..notice that the question states that he wants to see `$67.890` (with a dot as the thousands seperator) – ljgw Oct 13 '14 at 10:52
  • I know. But this might be a wrong decision so what I proposed here is the simplest and best and hence I stated: "Perhaps you meant this?" – shlomi33 Oct 13 '14 at 10:54
  • I said "might". Sometimes people put the wrong requirements on technical issues. I suggested, didn't say. My solution is simple enough for one to consider. Just trying to help... – shlomi33 Oct 13 '14 at 10:59
  • Perhaps you should convert your answer to a comment then:-) – ljgw Oct 13 '14 at 11:02
-2

Use BigDecimal for formatting instead of Long:

BigDecimal bd = BigDecimal.valueOf(amount, 3);
NumberFormat formatter = NumberFormat.getCurrencyInstance(new Locale("en","US"));
formatter.setMinimumFractionDigits(3);
String moneyString = formatter.format(bd);    
System.out.println(moneyString);
agad
  • 2,192
  • 1
  • 20
  • 32
  • That is extremely inefficient, as you are creating a new BigDecimal for each number solely for formatting. Using DecimalFormat you can reuse the formatting in a much more efficient (and less hackish) way. – oligofren Oct 13 '14 at 10:55
  • It's better, and I'll remove my downvote:-) but it does seem to be a roundabout way (especially since it uses FractionDigits). – ljgw Oct 13 '14 at 11:24
  • @ljgw 3 fraction digits is not currency format for US locale, so you need to adapt the standard format or use your own pattern: "¤#.000". – agad Oct 13 '14 at 12:32