Number format with FieldPosition : Number Format « Data Type « Java Tutorial






import java.text.FieldPosition;
import java.text.NumberFormat;

public class MainClass {

  public static void main(String[] args) {

    NumberFormat myFormat = NumberFormat.getNumberInstance();
    FieldPosition fp = new FieldPosition(NumberFormat.INTEGER_FIELD);
    myFormat.setMaximumIntegerDigits(3);
    myFormat.setMaximumFractionDigits(2);
    myFormat.setMinimumFractionDigits(2);

    System.out.println("Degrees  Radians  Grads");
    for (double d = 0.0; d < 360.0; d++) {
      String radianString = myFormat.format(Math.PI * d / 180.0, new StringBuffer(), fp)
          .toString();
      radianString = "   " + radianString;
      String gradString = myFormat.format(400 * d / 360, new StringBuffer(), fp).toString();
      gradString = "   " + gradString;
      String degreeString = myFormat.format(d, new StringBuffer(), fp).toString();
      degreeString = "   " + degreeString;
      System.out.println(degreeString + "  " + radianString + "  " + gradString);
    }
  }


}
Degrees  Radians  Grads
   0.00     0.00     0.00
   1.00     0.02     1.11
   2.00     0.03     2.22
   3.00     0.05     3.33
   4.00     0.07     4.44
   5.00     0.09     5.56
   6.00     0.10     6.67
   7.00     0.12     7.78
   8.00     0.14     8.89
   9.00     0.16     10.00
   10.00     0.17     11.11
   11.00     0.19     12.22
   12.00     0.21     13.33
   13.00     0.23     14.44
   14.00     0.24     15.56
   15.00     0.26     16.67
   16.00     0.28     17.78
   17.00     0.30     18.89
   18.00     0.31     20.00
   19.00     0.33     21.11
   20.00     0.35     22.22
   21.00     0.37     23.33
   22.00     0.38     24.44
   23.00     0.40     25.56
   24.00     0.42     26.67
   25.00     0.44     27.78
   26.00     0.45     28.89
   27.00     0.47     30.00
   28.00     0.49     31.11
   29.00     0.51     32.22
   30.00     0.52     33.33
   31.00     0.54     34.44
   32.00     0.56     35.56
   33.00     0.58     36.67
   34.00     0.59     37.78
   35.00     0.61     38.89
   36.00     0.63     40.00
   37.00     0.65     41.11
   38.00     0.66     42.22
   39.00     0.68     43.33
   40.00     0.70     44.44
   41.00     0.72     45.56
   42.00     0.73     46.67
   43.00     0.75     47.78
   44.00     0.77     48.89
   45.00     0.79     50.00
   46.00     0.80     51.11
   47.00     0.82     52.22
   48.00     0.84     53.33
   49.00     0.86     54.44
   50.00     0.87     55.56
   51.00     0.89     56.67
   52.00     0.91     57.78
   53.00     0.93     58.89
   54.00     0.94     60.00
   55.00     0.96     61.11
   56.00     0.98     62.22
   57.00     0.99     63.33
   58.00     1.01     64.44
   59.00     1.03     65.56
   60.00     1.05     66.67
   61.00     1.06     67.78
   62.00     1.08     68.89
   63.00     1.10     70.00
   64.00     1.12     71.11
   65.00     1.13     72.22
   66.00     1.15     73.33
   67.00     1.17     74.44
   68.00     1.19     75.56
   69.00     1.20     76.67
   70.00     1.22     77.78
   71.00     1.24     78.89
   72.00     1.26     80.00
   73.00     1.27     81.11
   74.00     1.29     82.22
   75.00     1.31     83.33
   76.00     1.33     84.44
   77.00     1.34     85.56
   78.00     1.36     86.67
   79.00     1.38     87.78
   80.00     1.40     88.89
   81.00     1.41     90.00
   82.00     1.43     91.11
   83.00     1.45     92.22
   84.00     1.47     93.33
   85.00     1.48     94.44
   86.00     1.50     95.56
   87.00     1.52     96.67
   88.00     1.54     97.78
   89.00     1.55     98.89
   90.00     1.57     100.00
   91.00     1.59     101.11
   92.00     1.61     102.22
   93.00     1.62     103.33
   94.00     1.64     104.44
   95.00     1.66     105.56
   96.00     1.68     106.67
   97.00     1.69     107.78
   98.00     1.71     108.89
   99.00     1.73     110.00
   100.00     1.75     111.11
   101.00     1.76     112.22
   102.00     1.78     113.33
   103.00     1.80     114.44
   104.00     1.82     115.56
   105.00     1.83     116.67
   106.00     1.85     117.78
   107.00     1.87     118.89
   108.00     1.88     120.00
   109.00     1.90     121.11
   110.00     1.92     122.22
   111.00     1.94     123.33
   112.00     1.95     124.44
   113.00     1.97     125.56
   114.00     1.99     126.67
   115.00     2.01     127.78
   116.00     2.02     128.89
   117.00     2.04     130.00
   118.00     2.06     131.11
   119.00     2.08     132.22
   120.00     2.09     133.33
   121.00     2.11     134.44
   122.00     2.13     135.56
   123.00     2.15     136.67
   124.00     2.16     137.78
   125.00     2.18     138.89
   126.00     2.20     140.00
   127.00     2.22     141.11
   128.00     2.23     142.22
   129.00     2.25     143.33
   130.00     2.27     144.44
   131.00     2.29     145.56
   132.00     2.30     146.67
   133.00     2.32     147.78
   134.00     2.34     148.89
   135.00     2.36     150.00
   136.00     2.37     151.11
   137.00     2.39     152.22
   138.00     2.41     153.33
   139.00     2.43     154.44
   140.00     2.44     155.56
   141.00     2.46     156.67
   142.00     2.48     157.78
   143.00     2.50     158.89
   144.00     2.51     160.00
   145.00     2.53     161.11
   146.00     2.55     162.22
   147.00     2.57     163.33
   148.00     2.58     164.44
   149.00     2.60     165.56
   150.00     2.62     166.67
   151.00     2.64     167.78
   152.00     2.65     168.89
   153.00     2.67     170.00
   154.00     2.69     171.11
   155.00     2.71     172.22
   156.00     2.72     173.33
   157.00     2.74     174.44
   158.00     2.76     175.56
   159.00     2.78     176.67
   160.00     2.79     177.78
   161.00     2.81     178.89
   162.00     2.83     180.00
   163.00     2.84     181.11
   164.00     2.86     182.22
   165.00     2.88     183.33
   166.00     2.90     184.44
   167.00     2.91     185.56
   168.00     2.93     186.67
   169.00     2.95     187.78
   170.00     2.97     188.89
   171.00     2.98     190.00
   172.00     3.00     191.11
   173.00     3.02     192.22
   174.00     3.04     193.33
   175.00     3.05     194.44
   176.00     3.07     195.56
   177.00     3.09     196.67
   178.00     3.11     197.78
   179.00     3.12     198.89
   180.00     3.14     200.00
   181.00     3.16     201.11
   182.00     3.18     202.22
   183.00     3.19     203.33
   184.00     3.21     204.44
   185.00     3.23     205.56
   186.00     3.25     206.67
   187.00     3.26     207.78
   188.00     3.28     208.89
   189.00     3.30     210.00
   190.00     3.32     211.11
   191.00     3.33     212.22
   192.00     3.35     213.33
   193.00     3.37     214.44
   194.00     3.39     215.56
   195.00     3.40     216.67
   196.00     3.42     217.78
   197.00     3.44     218.89
   198.00     3.46     220.00
   199.00     3.47     221.11
   200.00     3.49     222.22
   201.00     3.51     223.33
   202.00     3.53     224.44
   203.00     3.54     225.56
   204.00     3.56     226.67
   205.00     3.58     227.78
   206.00     3.60     228.89
   207.00     3.61     230.00
   208.00     3.63     231.11
   209.00     3.65     232.22
   210.00     3.67     233.33
   211.00     3.68     234.44
   212.00     3.70     235.56
   213.00     3.72     236.67
   214.00     3.74     237.78
   215.00     3.75     238.89
   216.00     3.77     240.00
   217.00     3.79     241.11
   218.00     3.80     242.22
   219.00     3.82     243.33
   220.00     3.84     244.44
   221.00     3.86     245.56
   222.00     3.87     246.67
   223.00     3.89     247.78
   224.00     3.91     248.89
   225.00     3.93     250.00
   226.00     3.94     251.11
   227.00     3.96     252.22
   228.00     3.98     253.33
   229.00     4.00     254.44
   230.00     4.01     255.56
   231.00     4.03     256.67
   232.00     4.05     257.78
   233.00     4.07     258.89
   234.00     4.08     260.00
   235.00     4.10     261.11
   236.00     4.12     262.22
   237.00     4.14     263.33
   238.00     4.15     264.44
   239.00     4.17     265.56
   240.00     4.19     266.67
   241.00     4.21     267.78
   242.00     4.22     268.89
   243.00     4.24     270.00
   244.00     4.26     271.11
   245.00     4.28     272.22
   246.00     4.29     273.33
   247.00     4.31     274.44
   248.00     4.33     275.56
   249.00     4.35     276.67
   250.00     4.36     277.78
   251.00     4.38     278.89
   252.00     4.40     280.00
   253.00     4.42     281.11
   254.00     4.43     282.22
   255.00     4.45     283.33
   256.00     4.47     284.44
   257.00     4.49     285.56
   258.00     4.50     286.67
   259.00     4.52     287.78
   260.00     4.54     288.89
   261.00     4.56     290.00
   262.00     4.57     291.11
   263.00     4.59     292.22
   264.00     4.61     293.33
   265.00     4.63     294.44
   266.00     4.64     295.56
   267.00     4.66     296.67
   268.00     4.68     297.78
   269.00     4.69     298.89
   270.00     4.71     300.00
   271.00     4.73     301.11
   272.00     4.75     302.22
   273.00     4.76     303.33
   274.00     4.78     304.44
   275.00     4.80     305.56
   276.00     4.82     306.67
   277.00     4.83     307.78
   278.00     4.85     308.89
   279.00     4.87     310.00
   280.00     4.89     311.11
   281.00     4.90     312.22
   282.00     4.92     313.33
   283.00     4.94     314.44
   284.00     4.96     315.56
   285.00     4.97     316.67
   286.00     4.99     317.78
   287.00     5.01     318.89
   288.00     5.03     320.00
   289.00     5.04     321.11
   290.00     5.06     322.22
   291.00     5.08     323.33
   292.00     5.10     324.44
   293.00     5.11     325.56
   294.00     5.13     326.67
   295.00     5.15     327.78
   296.00     5.17     328.89
   297.00     5.18     330.00
   298.00     5.20     331.11
   299.00     5.22     332.22
   300.00     5.24     333.33
   301.00     5.25     334.44
   302.00     5.27     335.56
   303.00     5.29     336.67
   304.00     5.31     337.78
   305.00     5.32     338.89
   306.00     5.34     340.00
   307.00     5.36     341.11
   308.00     5.38     342.22
   309.00     5.39     343.33
   310.00     5.41     344.44
   311.00     5.43     345.56
   312.00     5.45     346.67
   313.00     5.46     347.78
   314.00     5.48     348.89
   315.00     5.50     350.00
   316.00     5.52     351.11
   317.00     5.53     352.22
   318.00     5.55     353.33
   319.00     5.57     354.44
   320.00     5.59     355.56
   321.00     5.60     356.67
   322.00     5.62     357.78
   323.00     5.64     358.89
   324.00     5.65     360.00
   325.00     5.67     361.11
   326.00     5.69     362.22
   327.00     5.71     363.33
   328.00     5.72     364.44
   329.00     5.74     365.56
   330.00     5.76     366.67
   331.00     5.78     367.78
   332.00     5.79     368.89
   333.00     5.81     370.00
   334.00     5.83     371.11
   335.00     5.85     372.22
   336.00     5.86     373.33
   337.00     5.88     374.44
   338.00     5.90     375.56
   339.00     5.92     376.67
   340.00     5.93     377.78
   341.00     5.95     378.89
   342.00     5.97     380.00
   343.00     5.99     381.11
   344.00     6.00     382.22
   345.00     6.02     383.33
   346.00     6.04     384.44
   347.00     6.06     385.56
   348.00     6.07     386.67
   349.00     6.09     387.78
   350.00     6.11     388.89
   351.00     6.13     390.00
   352.00     6.14     391.11
   353.00     6.16     392.22
   354.00     6.18     393.33
   355.00     6.20     394.44
   356.00     6.21     395.56
   357.00     6.23     396.67
   358.00     6.25     397.78
   359.00     6.27     398.89








2.14.Number Format
2.14.1.Number formatting helps make your numbers more readable.
2.14.2.Specifying Precision
2.14.3.Applied to strings, the precision specifier specifies the maximum field length
2.14.4.Illustrating the precision specifier
2.14.5.Add leading zeros to a number
2.14.6.NumberFormat.getInstance()
2.14.7.NumberFormat.getCurrencyInstance(Locale.ENGLISH)
2.14.8.NumberFormat: Minimum Integer Digits, Maximum/Minimum Fraction Digits
2.14.9.Number format with FieldPosition
2.14.10.NumberFormat.getPercentInstance(Locale.ENGLISH)
2.14.11.A number formatter for logarithmic values. This formatter does not support parsing.
2.14.12.Format a percentage for presentation to the user
2.14.13.Get Percent Value
2.14.14.Helper class for format number and currency