Code Snippets

  

Java Source Code


Welcome to Dream.In.Code
Become a Java Expert!

Join 149,019 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 2,020 people online right now. 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: 13,078

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.




Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month