Username: Save?
Password:
Home Forum Links Search Login Register*
    News: Welcome to the TechnoWorldInc! Community!
Recent Updates
[April 24, 2024, 11:48:22 AM]

[April 24, 2024, 11:48:22 AM]

[April 24, 2024, 11:48:22 AM]

[April 24, 2024, 11:48:22 AM]

[April 03, 2024, 06:11:00 PM]

[April 03, 2024, 06:11:00 PM]

[April 03, 2024, 06:11:00 PM]

[April 03, 2024, 06:11:00 PM]

[March 06, 2024, 02:45:27 PM]

[March 06, 2024, 02:45:27 PM]

[March 06, 2024, 02:45:27 PM]

[March 06, 2024, 02:45:27 PM]

[February 14, 2024, 02:00:39 PM]
Subscriptions
Get Latest Tech Updates For Free!
Resources
   Travelikers
   Funistan
   PrettyGalz
   Techlap
   FreeThemes
   Videsta
   Glamistan
   BachatMela
   GlamGalz
   Techzug
   Vidsage
   Funzug
   WorldHostInc
   Funfani
   FilmyMama
   Uploaded.Tech
   MegaPixelShop
   Netens
   Funotic
   FreeJobsInc
   FilesPark
Participate in the fastest growing Technical Encyclopedia! This website is 100% Free. Please register or login using the login box above if you have already registered. You will need to be logged in to reply, make new topics and to access all the areas. Registration is free! Click Here To Register.
+ Techno World Inc - The Best Technical Encyclopedia Online! » Forum » THE TECHNO CLUB [ TECHNOWORLDINC.COM ] » Programming Zone » JAVA
 Java: Decimal Format to Easily Create Custom Output
Pages: [1]   Go Down
  Print  
Author Topic: Java: Decimal Format to Easily Create Custom Output  (Read 2482 times)
Khushi
Global Moderator
Adv. Member
*****



Karma: 4
Offline Offline

Posts: 329

Hello!!


View Profile
Java: Decimal Format to Easily Create Custom Output
« Posted: January 03, 2007, 12:08:09 AM »


The Decimal Format class is a poorly explained class in the java Documentation so i will clear up a little of it with an example.

you first need to import the DecimalFormat class:

Code:
import java.text.DecimalFormat;

then create a 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.
*This example will be used as a standard format of money:

Code:
DecimalFormat money = new DecimalFormat("$0.00");

Now that you have a 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:

Code:
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!

Logged

Pages: [1]   Go Up
  Print  
 
Jump to:  

Copyright © 2006-2023 TechnoWorldInc.com. All Rights Reserved. Privacy Policy | Disclaimer
Page created in 0.134 seconds with 24 queries.