Anupam Sharma
Anupam Sharma

Reputation: 1569

Double value with two decimal using NumberFormat

I am using java.text.NumberFormat to convert my double value to two decimal with conman separate format.

My code of line is:

NumberFormat.getNumberInstance(Locale.US).format(53508));

This code will convert 53508 to 53,508 format but I require in 53,508.00 format (with 2 decimal).

Upvotes: 12

Views: 10021

Answers (3)

Jéwôm'
Jéwôm'

Reputation: 3971

Try this in Kotlin :

val n = NumberFormat.getNumberInstance(Locale.US)
n.maximumFractionDigits = 2
n.minimumFractionDigits = 2

And after format your number with :

n.format(53508)

Upvotes: 0

Anupam Sharma
Anupam Sharma

Reputation: 1569

Thanks Friends I solve it .... :)

Below is line of code

NumberFormat formatter = NumberFormat.getInstance(Locale.US);
formatter.setMaximumFractionDigits(2);
formatter.setMinimumFractionDigits(2);

System.out.prinitln(formatter.format(53508));

and output is

53,508.00

Upvotes: 11

Evgeniy Dorofeev
Evgeniy Dorofeev

Reputation: 135992

try this

    NumberFormat f = NumberFormat.getNumberInstance(Locale.US);
    f.setMinimumFractionDigits(2);
    String s = f.format(53508);

Upvotes: 3

Related Questions