Link to home
Start Free TrialLog in
Avatar of gudii9
gudii9Flag for United States of America

asked on

number formatting in java

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
ASKER CERTIFIED SOLUTION
Avatar of Ken Butters
Ken Butters
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gudii9

ASKER

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
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.
Avatar of gudii9

ASKER

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
Avatar of gudii9

ASKER

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
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
Avatar of gudii9

ASKER

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.