André Long Ngo
André Long Ngo

Reputation: 11

Java decimal point given by user

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.

Upvotes: 0

Views: 50

Answers (2)

Hasan Aslam
Hasan Aslam

Reputation: 799

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

Upvotes: 1

hubert.volz
hubert.volz

Reputation: 1

I would assume that

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

does the trick.

Upvotes: 0

Related Questions