0

How to printf decimal point by user? For example:

float a = 122.32445;

int decimal = 2;

And I want to print 122.32 I know that exist:

System.out.printf("%.2f", a);    

But how to implement that decimal point there? I came up with something like this:

System.out.printf("%.%df, decimal, a);

But it of course it doesn't work. Thanks for any help.

  • 1
    Possible duplicate of [Format Float to n decimal places](http://stackoverflow.com/questions/5195837/format-float-to-n-decimal-places) – Tom Mar 05 '17 at 21:28

2 Answers2

1
System.out.printf("%." + decimal + "f", a);
Hasan Aslam
  • 789
  • 6
  • 14
0

I would assume that

DecimalFormat decimalFormat = new DecimalFormat("##,##0.00");

does the trick.