Code Snippets

  

Java Source Code


Welcome to Dream.In.Code
Click Here
Getting Java Help is Easy!

Join 117,542 Java Programmers for FREE! Ask your question and get quick answers from experts. There are 1,663 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!




DecimalFormat

How to impliment the decimal format class with an example

Submitted By: William_Wilson
Actions:
Rating:
Views: 10,517

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


  1. //you first need to import the DecimalFormat class:
  2. import java.text.DecimalFormat;
  3.  
  4. //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.
  5. //This example will be used as a standard format of money:
  6.  
  7. DecimalFormat money = new DecimalFormat("$0.00");
  8.  
  9. //Now that you have a money object. (declared either globally or locally) you can now [b]format[/b] your objects
  10. //Note this example uses only 2 decimal places and a $, but any symbols or length is available.
  11.  
  12. //As an example of prniting a double value:
  13. double amount = 4.333333333;
  14. System.out.println(money.format(amount));
  15.  
  16. //what will be printed is: $4.33 as described by the template, this handles your percision and dollar sign with a single template!
  17.  

Copy & Paste


Comments


jhonikhel 2008-09-30 05:55:04

ok the program is good

jhonikhel 2008-09-30 05:55:58

hey can i ask you something about decimal to binary convertion using java applet?? thanks

jhonikhel 2008-09-30 05:56:25

can you please help me to my ass.???

William_Wilson 2008-09-30 05:59:49

Post in the forums, that's what they're for.


Add comment


You must be registered and logged on to </dream.in.code> to leave comments.





Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month