Beginner
Beginner

Reputation: 875

Number formatting in Java

I need the following number format in java:

long a=5000;

Expected output is : 50,00

if

long a = 25624;

Expected output is : 256,24

long a = 614324;

Expected output is

6143,24

How can I get this in java?

Upvotes: 1

Views: 410

Answers (3)

Pankti
Pankti

Reputation: 419

Formatter class in java is used to create formatted output. To do so, the format() method is used. Syntax of format() method is as shown below:

Formatter format(String fmtString, Object ... args)

For the number format in your program the following can be done:

Formatter f = new Formatter();
f.format("%d,%2d",a/100,a%100);

Upvotes: 0

Peter Perháč
Peter Perháč

Reputation: 20792

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

Upvotes: -1

dagnelies
dagnelies

Reputation: 5329

String.format("%d,%02d",a/100, a%100)

Upvotes: 9

Related Questions