Solved

number formatting in java

Posted on 2014-02-04
8
549 Views
Last Modified: 2014-02-13
Hi,

I am looking at below example
http://www.avajava.com/tutorials/lessons/how-do-i-use-numberformat-to-format-doubles.html

I wonder what is math.pow function is. I have not understood below output
 how we got it

please advise
Any links resources ideas highly appreciated. Thanks in advance


Format:default
  formatted:0 | unformatted:0.0
  formatted:3.142 | unformatted:3.141592653589793
  formatted:25.133 | unformatted:25.132741228718345
  formatted:254.469 | unformatted:254.46900494077323
  formatted:3,216.991 | unformatted:3216.990877275948
  formatted:49,087.385 | unformatted:49087.385212340516
  formatted:879,444.881 | unformatted:879444.8810753123
  formatted:18,110,656.471 | unformatted:1.8110656471007094E7
  formatted:421,657,428.266 | unformatted:4.216574282663131E8
  formatted:10,954,056,258.833 | unformatted:1.0954056258833088E10
  formatted:314,159,265,358.979 | unformatted:3.141592653589793E11

Format:min fraction digits 2, max fraction digits 4
  formatted:0.00 | unformatted:0.0
  formatted:3.1416 | unformatted:3.141592653589793
  formatted:25.1327 | unformatted:25.132741228718345
  formatted:254.469 | unformatted:254.46900494077323
  formatted:3,216.9909 | unformatted:3216.990877275948
  formatted:49,087.3852 | unformatted:49087.385212340516
  formatted:879,444.8811 | unformatted:879444.8810753123
  formatted:18,110,656.471 | unformatted:1.8110656471007094E7
  formatted:421,657,428.2663 | unformatted:4.216574282663131E8
  formatted:10,954,056,258.8331 | unformatted:1.0954056258833088E10
  formatted:314,159,265,358.9793 | unformatted:3.141592653589793E11

Format:min integer digits 6
  formatted:000,000 | unformatted:0.0
  formatted:000,003.142 | unformatted:3.141592653589793
  formatted:000,025.133 | unformatted:25.132741228718345
  formatted:000,254.469 | unformatted:254.46900494077323
  formatted:003,216.991 | unformatted:3216.990877275948
  formatted:049,087.385 | unformatted:49087.385212340516
  formatted:879,444.881 | unformatted:879444.8810753123
  formatted:18,110,656.471 | unformatted:1.8110656471007094E7
  formatted:421,657,428.266 | unformatted:4.216574282663131E8
  formatted:10,954,056,258.833 | unformatted:1.0954056258833088E10
  formatted:314,159,265,358.979 | unformatted:3.141592653589793E11

Format:max integer digits 5
  formatted:0 | unformatted:0.0
  formatted:3.142 | unformatted:3.141592653589793
  formatted:25.133 | unformatted:25.132741228718345
  formatted:254.469 | unformatted:254.46900494077323
  formatted:3,216.991 | unformatted:3216.990877275948
  formatted:49,087.385 | unformatted:49087.385212340516
  formatted:79,444.881 | unformatted:879444.8810753123
  formatted:10,656.471 | unformatted:1.8110656471007094E7
  formatted:57,428.266 | unformatted:4.216574282663131E8
  formatted:56,258.833 | unformatted:1.0954056258833088E10
  formatted:65,358.979 | unformatted:3.141592653589793E11

Format:grouping off
  formatted:0 | unformatted:0.0
  formatted:3.142 | unformatted:3.141592653589793
  formatted:25.133 | unformatted:25.132741228718345
  formatted:254.469 | unformatted:254.46900494077323
  formatted:3216.991 | unformatted:3216.990877275948
  formatted:49087.385 | unformatted:49087.385212340516
  formatted:879444.881 | unformatted:879444.8810753123
  formatted:18110656.471 | unformatted:1.8110656471007094E7
  formatted:421657428.266 | unformatted:4.216574282663131E8
  formatted:10954056258.833 | unformatted:1.0954056258833088E10
  formatted:314159265358.979 | unformatted:3.141592653589793E11
0
Comment
Question by:gudii9
  • 4
  • 3
8 Comments
 
LVL 19

Accepted Solution

by:
Ken Butters earned 300 total points
Comment Utility
Here is a resource for math.pow

http://docs.oracle.com/javase/6/docs/api/java/lang/Math.html

Here is excerpt from above page:

static double      pow(double a, double b)
          Returns the value of the first argument raised to the power of the second argument.



In the loop in your example
		for (int i = 0; i <= 10; i++) {
			double num = Math.PI * Math.pow(i, i) * i;
			System.out.print("  formatted:" + numberFormat.format(num));
			System.out.println(" | unformatted:" + num);
		}

Open in new window

they are counting from zero to ten.

each iteration through the loop assigns a new value to num

so just concentrating of the math.pow part of the loop...

on the first iteration they are taking zero and raising it to the power of zero which equals 1
on the 2nd iteration they take one and raise it to the power of one which equals 1
on the 3rd iteration they take two and raise it to the power of two which equals 4
on the 4th iteration they take three and raise it to the power of three which equals 27

This continues on for the remainder of the loop.

Note that the above is ONLY for the math.pow part of the equation.  they also take the result of it times 0, 1, 2, 3 and times the value of PI for each iteration as well.

So completing the first 4 iteration computations:

where the math.pow result is:

1
1
4
27

We also need to apply the remaining part of the equation giving these approx values (approx because I used 3.14 instead of Math.PI)

3.14 * 1 * 0  = 0
3.14 * 1 * 1  = 3.14
3.14 * 4 * 2 = 25.12
3.14 * 27 * 3 = 254.34
0
 
LVL 7

Author Comment

by:gudii9
Comment Utility
That makes more sense.

Format:min integer digits 6
Format:min integer digits 5

what is the difference between above two loops. Why they have separate loops for 'default' and then also 'off'. please advise
0
 
LVL 19

Expert Comment

by:Ken Butters
Comment Utility
The reason for the separate calls to displayNumbers is to demonstrate how to use the class "NumberFormat".

Here is java page for NumberFormat: http://docs.oracle.com/javase/7/docs/api/java/text/NumberFormat.html

You start execution in your main routine with this code:
NumberFormat format1 = NumberFormat.getInstance();

Open in new window

All this code does is create an instance of NumberFormat Class.  This is going to be one of the parameters passed to displayNumbers.

DisplayNumbers itself takes 2 parameters.

1st Parameter - is a string to display... like a descriptive header.
2nd Parameter - is an instance of NumberFormat

Then in the display routine... it loops from zero to ten every time it is called as described in previous post in this question.

Each time DisplayNumbers is called... it passes in a different instance of "NumberFormat" so that you can visualize how the number formatting impacts the display of numbers.

The first call displays what it looks like when you didn't do anything with NumberFormat object.
Format:default
  formatted:0 | unformatted:0.0
  formatted:3.142 | unformatted:3.141592653589793
  formatted:25.133 | unformatted:25.132741228718345
  formatted:254.469 | unformatted:254.46900494077323
  formatted:3,216.991 | unformatted:3216.990877275948
  formatted:49,087.385 | unformatted:49087.385212340516
  formatted:879,444.881 | unformatted:879444.8810753123
  formatted:18,110,656.471 | unformatted:1.8110656471007094E7
  formatted:421,657,428.266 | unformatted:4.216574282663131E8
  formatted:10,954,056,258.833 | unformatted:1.0954056258833088E10
  formatted:314,159,265,358.979 | unformatted:3.141592653589793E11 

Open in new window

The second call they changed NumberFormat so that these 2 lines were executed:
		format2.setMinimumFractionDigits(2);
		format2.setMaximumFractionDigits(4);

Open in new window


(the above instructs the formatting to use a minimum of 2 decimal places and maximum of 4).

The resulting display is shown with this output:
Note in this output... the unformatted number doesn't change... but the formatted number now has a minimum of 2 decimal places and a maximum of 4.

minimum is shown by "0.00"
Maximum is shown by all other rows:  3.1416 for example   the 1416 to the right of the decimal is the max # of decimal places that this formatting allows.

Format:min fraction digits 2, max fraction digits 4
  formatted:0.00 | unformatted:0.0
  formatted:3.1416 | unformatted:3.141592653589793
  formatted:25.1327 | unformatted:25.132741228718345
  formatted:254.469 | unformatted:254.46900494077323
  formatted:3,216.9909 | unformatted:3216.990877275948
  formatted:49,087.3852 | unformatted:49087.385212340516
  formatted:879,444.8811 | unformatted:879444.8810753123
  formatted:18,110,656.471 | unformatted:1.8110656471007094E7
  formatted:421,657,428.2663 | unformatted:4.216574282663131E8
  formatted:10,954,056,258.8331 | unformatted:1.0954056258833088E10
  formatted:314,159,265,358.9793 | unformatted:3.141592653589793E11

Open in new window

Each subsequent call shows a different setting for NumberFormat.
0
 
LVL 7

Author Comment

by:gudii9
Comment Utility
NumberFormat is the abstract base class for all number formats. This class provides the interface for formatting and parsing numbers

what is the meaning of parsing the numbers. Please advise
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 7

Author Comment

by:gudii9
Comment Utility
DisplayNumbers itself takes 2 parameters.



I do not see the above method in below link
http://docs.oracle.com/javase/7/docs/api/java/text/NumberFormat.html



Where that method is defined.

format2.setMinimumFractionDigits(2);
            format2.setMaximumFractionDigits(4);
format2.setMinimumFractionDigits(2);
            format2.setMaximumFractionDigits(4);


what is meaning of setting maximum fraction digits.

Each subsequent call shows a different setting for NumberFormat.
what is the meaning of setting for format.

Please advise
0
 
LVL 35

Assisted Solution

by:mccarl
mccarl earned 200 total points
Comment Utility
what is the meaning of parsing the numbers.
'Parsing' a number means to take the String representation of the number, ie. String numString = "42.7", and convert that into a numerical data type, so the result would be the same as doing...   double numDouble = 42.7;


DisplayNumbers itself takes 2 parameters.

I do not see the above method in below link
http://docs.oracle.com/javase/7/docs/api/java/text/NumberFormat.html
That's because it isn't a method of NumberFormat, it is just the method that they defined in the tutorial that you linked to. Take a look at the code in that tutorial again.


what is meaning of setting maximum fraction digits
Setting the maximum fraction digits, affects how the conversion from a number to a String happens, eg. in the above they are setting 4 as the max fraction digits on the formatter, so say you had...    double num = 98.7654321;   and then you formatted with the above formatter, the result would be a String that contained... "98.7654" ie. it has only included 4 fraction digits or in other words, 4 decimal places.
0
 
LVL 19

Expert Comment

by:Ken Butters
Comment Utility
gudii9.... this is getting ridiculous.

You need to give credit when a question is answered.

You seem to want to ask an endless supply of questions IN JUST ONE QUESTION.

In this question you have asked the following:

I wonder what is math.pow function is. I have not understood below output
 how we got it

what is the difference between above two loops. Why they have separate loops for 'default' and then also 'off'. please advise

what is the meaning of parsing the numbers. Please advise

what is meaning of setting maximum fraction digits.

This is a never ending cycle.  I and other experts have also answered many of your other questions which you never seem to want to close or give credit for.

You need to decide whether the original question has been answered or not.  If it has been answered you need to give credit for it.

If it has not been answered you need to ask for clarification on it.

If you have a different question (which you obviously have in endless supply) then you need to open up an entirely new question.
0
 
LVL 7

Author Comment

by:gudii9
Comment Utility
I see they generated some number and then applying their own formatting with help of API mthods like set Grouping off, set Max fraction digits etc.
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
topping2 challenge 13 56
topping3 challenge 14 48
What is the latest versions eclipse neon 2 116
Java asynchronous logging 4 31
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now