Go4Expert (http://www.go4expert.com/)
-   Java (http://www.go4expert.com/articles/java-tutorials/)
-   -   Number formatting with DecimalFormat (http://www.go4expert.com/articles/formatting-decimalformat-t2392/)

pradeep 30Dec2006 14:02

Number formatting with DecimalFormat
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:

Code: Java

import java.text.DecimalFormat;

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:

Code: Java

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

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:

Code: Java

double amount = 4.333333333;

The output is: $4.33 as described by the template, this handles your percision and dollar sign with a single template.

kiddo 30Apr2009 13:37

Re: Number formatting with DecimalFormat

I've a question.... maybe some :p
I got a project, maybe not a project,
just a small programme.

I want to make it looks like a table,
but everytime I run it,
the position of the coulumn and row weren't fit,
it wasn't look like a table.

Can you tell me how can I manipulate the output,
so it will look neat?

E.g :

Name Phone Address
one 001101 one street
~~~ ~~~~~ ~~~~~~
~~~ ~~~~~ ~~~~~~
~~~ ~~~~~ ~~~~~~


Sorry if my English not so good.
I'll improve it.

All times are GMT +5.5. The time now is 03:31.