What's Here?
- Members: 300,313
- Replies: 825,542
- Topics: 137,367
- Snippets: 4,417
- Tutorials: 1,147
- Total Online: 2,057
- Members: 108
- Guests: 1,949
|
How to impliment the decimal format class with an example
|
Submitted By: William_Wilson
|
|
Rating:
 
|
|
Views: 20,584 |
Language: Java
|
|
Last Modified: September 9, 2008 |
|
Instructions: copy code sections and modify to your needs, decimalformat uses a string template to design your output. |
Snippet
//you first need to import the DecimalFormat class:
import java.text.DecimalFormat;
//then create a format object. this object can be used with doubles, as it uses a [i]decimal[/i]. It uses a template String to [b]teach[/b] Java how to ouput the objects.
//This example will be used as a standard format of money:
//Now that you have a money object. (declared either globally or locally) you can now [b]format[/b] 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:
double amount = 4.333333333;
System. out. println(money. format(amount ));
//what will be printed is: $4.33 as described by the template, this handles your percision and dollar sign with a single template!
Copy & Paste
|
|
|
|