The Decimal Format class is not very well documented, so I will try and exmplain it with an example.
Firstly, you need to import the DecimalFormat class:
Create an instance the format object. This object can be used with doubles, as it uses a decimal. It uses a template String to teach Java how to ouput the objects.
I am using this as a standard format of money:
Now that you have the money object. (declared either globally or locally) you can now format your objects
Note: This example uses only 2 decimal places and a $, but any symbols or length is available.
As an example of prniting a double value:
The output is: $4.33 as described by the template, this handles your percision and dollar sign with a single template.
Firstly, you need to import the DecimalFormat class:
Code: Java
import java.text.DecimalFormat;
I am using this as a standard format of money:
Code: Java
DecimalFormat money = new DecimalFormat("$0.00");
Note: This example uses only 2 decimal places and a $, but any symbols or length is available.
As an example of prniting a double value:
Code: Java
double amount = 4.333333333;
System.out.println(money.format(amount));

