Monday, June 11, 2007

Formatting a number to a fixed number of decimal places

We can use a DecimalFormat object to format a given number to a fixed number of places.
Here is the example that does the trick

import java.text.DecimalFormat;
import java.text.ParseException;

public class TestClass {
public static void main(String[] args) throws ParseException {
double doubleAmount_ = 10000;

DecimalFormat currency = new DecimalFormat("$0.00 dollars");

// Without using DecimalFormat class and hence without using fixed no. of decimal places.
Float result = (float) doubleAmount_ / 66;
System.out.println("$" + result + " dollars");
// Now using DecimalFormat class and hence fixing no.of decimal places to two.
String formattedResult = currency.format(result);
System.out.println(formattedResult);

// parsing a float value from a String using a DecimalFormat class.
String strAmount = "$10000.00 dollars";
Float floatValue = currency.parse(strAmount).floatValue();
System.out.println(floatValue);
}
}

Here are the results:

$151.51515 dollars
$151.52 dollars
10000.0

In the constructor of DecimalFormat we pass the format string, according to which the input value will be formatted.
I have demonstrated two ways in which divison is performed. First one is normal divison and hence leads to unformatted
output 151.51515, but in second the division is formatted upto two decimal places using format method of DecimalFormat
class.

Further we can use DecimalFormat class to parse a number from a formatted string like done above. In this case we use
parse method of DecimalFormat class. this gives us the output of 10000.0 when given an input string "$10000.00 dollars".

We can typically encapsulate the DecimalFormat object within a class, so that each time we do not need to pass the DecimalFormat
class instance. Typically we use toString method to format the results.

Using this model, here's the example

public class TestClass {

public static void main(String[] args) {
double totalAmount = 10000.0;
DecimalFormatTest amount = new DecimalFormatTest(totalAmount / 66);
System.out.println(amount);
}
}


class DecimalFormatTest {
double amount_;

public DecimalFormatTest(double amount) {
amount_ = amount;
}

public String toString() {
DecimalFormat currency = new DecimalFormat("$0.00 dollars");
return currency.format(amount_);
}
}


It produces the following output
$151.52 dollars

Sphere: Related Content

0 comments: