annotate libs/commons-math-2.1/docs/userguide/fraction.html @ 10:5f2c5fb36e93

commons-math-2.1 added
author dwinter
date Tue, 04 Jan 2011 10:00:53 +0100
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
10
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
2
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
3
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
4
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
5
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
6
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
7
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
8
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
9
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
10
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
11
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
12
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
13 <html xmlns="http://www.w3.org/1999/xhtml">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
14 <head>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
15 <title>Math - The Commons Math User Guide - Fractions</title>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
16 <style type="text/css" media="all">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
17 @import url("../css/maven-base.css");
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
18 @import url("../css/maven-theme.css");
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
19 @import url("../css/site.css");
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
20 </style>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
21 <link rel="stylesheet" href="../css/print.css" type="text/css" media="print" />
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
22 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
23 </head>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
24 <body class="composite">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
25 <div id="banner">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
26 <span id="bannerLeft">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
27
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
28 Commons Math User Guide
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
29
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
30 </span>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
31 <div class="clear">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
32 <hr/>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
33 </div>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
34 </div>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
35 <div id="breadcrumbs">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
36
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
37
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
38
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
39
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
40
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
41
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
42
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
43
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
44 <div class="xright">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
45
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
46
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
47
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
48
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
49
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
50
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
51
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
52 </div>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
53 <div class="clear">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
54 <hr/>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
55 </div>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
56 </div>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
57 <div id="leftColumn">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
58 <div id="navcolumn">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
59
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
60
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
61
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
62
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
63
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
64
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
65
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
66
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
67 <h5>User Guide</h5>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
68 <ul>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
69
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
70 <li class="none">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
71 <a href="../userguide/index.html">Contents</a>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
72 </li>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
73
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
74 <li class="none">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
75 <a href="../userguide/overview.html">Overview</a>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
76 </li>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
77
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
78 <li class="none">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
79 <a href="../userguide/stat.html">Statistics</a>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
80 </li>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
81
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
82 <li class="none">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
83 <a href="../userguide/random.html">Data Generation</a>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
84 </li>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
85
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
86 <li class="none">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
87 <a href="../userguide/linear.html">Linear Algebra</a>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
88 </li>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
89
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
90 <li class="none">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
91 <a href="../userguide/analysis.html">Numerical Analysis</a>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
92 </li>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
93
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
94 <li class="none">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
95 <a href="../userguide/special.html">Special Functions</a>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
96 </li>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
97
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
98 <li class="none">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
99 <a href="../userguide/utilities.html">Utilities</a>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
100 </li>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
101
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
102 <li class="none">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
103 <a href="../userguide/complex.html">Complex Numbers</a>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
104 </li>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
105
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
106 <li class="none">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
107 <a href="../userguide/distribution.html">Distributions</a>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
108 </li>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
109
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
110 <li class="none">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
111 <strong>Fractions</strong>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
112 </li>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
113
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
114 <li class="none">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
115 <a href="../userguide/transform.html">Transform Methods</a>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
116 </li>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
117
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
118 <li class="none">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
119 <a href="../userguide/geometry.html">3D Geometry</a>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
120 </li>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
121
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
122 <li class="none">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
123 <a href="../userguide/optimization.html">Optimization</a>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
124 </li>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
125
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
126 <li class="none">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
127 <a href="../userguide/ode.html">Ordinary Differential Equations</a>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
128 </li>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
129
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
130 <li class="none">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
131 <a href="../userguide/genetics.html">Genetic Algorithms</a>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
132 </li>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
133 </ul>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
134 <a href="http://maven.apache.org/" title="Built by Maven" class="poweredBy">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
135 <img alt="Built by Maven" src="../images/logos/maven-feather.png"></img>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
136 </a>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
137
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
138
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
139
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
140
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
141
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
142
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
143
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
144
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
145 </div>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
146 </div>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
147 <div id="bodyColumn">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
148 <div id="contentBox">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
149 <div class="section"><h2><a name="a9_Fractions"></a>9 Fractions</h2>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
150 <div class="section"><h3><a name="a9.1_Overview"></a>9.1 Overview</h3>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
151 <p>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
152 The fraction packages provides a fraction number type as well as
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
153 fraction number formatting.
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
154 </p>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
155 </div>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
156 <div class="section"><h3><a name="a9.2_Fraction_Numbers"></a>9.2 Fraction Numbers</h3>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
157 <p><a href="../apidocs/org/apache/commons/math/fraction/Fraction.html">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
158 org.apache.commons.math.fraction.Fraction</a> and
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
159 <a href="../apidocs/org/apache/commons/math/fraction/BigFraction.html">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
160 org.apache.commons.math.fraction.BigFraction</a> provide fraction number
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
161 type that forms the basis for the fraction functionality found in
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
162 commons-math. The former one can be used for fractions whose numerators
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
163 and denominators are small enough to fit in an int (taking care of intermediate
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
164 values) while the second class should be used when there is a risk the numerator
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
165 and denominator grow very large.
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
166 </p>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
167 <p>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
168 A fraction number, can be built from two integer arguments representing numerator
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
169 and denominator or from a double which will be approximated:
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
170 <div class="source"><pre>Fraction f = new Fraction(1, 3); // 1 / 3
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
171 Fraction g = new Fraction(0.25); // 1 / 4</pre>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
172 </div>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
173 </p>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
174 <p>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
175 Of special note with fraction construction, when a fraction is created it is always reduced to lowest terms.
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
176 </p>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
177 <p>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
178 The <code>Fraction</code> class provides many unary and binary
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
179 fraction operations. These operations provide the means to add,
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
180 subtract, multiple and, divide fractions along with other functions similar to the real number functions found in
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
181 <code>java.math.BigDecimal</code>:
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
182 <div class="source"><pre>Fraction lhs = new Fraction(1, 3);
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
183 Fraction rhs = new Fraction(2, 5);
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
184
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
185 Fraction answer = lhs.add(rhs); // add two fractions
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
186 answer = lhs.subtract(rhs); // subtract two fractions
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
187 answer = lhs.abs(); // absolute value
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
188 answer = lhs.reciprocal(); // reciprocal of lhs</pre>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
189 </div>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
190 </p>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
191 <p>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
192 Like fraction construction, for each of the fraction functions, the resulting fraction is reduced to lowest terms.
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
193 </p>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
194 </div>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
195 <div class="section"><h3><a name="a9.3_Fraction_Formatting_and_Parsing"></a>9.3 Fraction Formatting and Parsing</h3>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
196 <p><code>Fraction</code> instances can be converted to and from strings
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
197 using the<a href="../apidocs/org/apache/commons/math/fraction/FractionFormat.html">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
198 org.apache.commons.math.fraction.FractionFormat</a> class.
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
199 <code>FractionFormat</code> is a <code>java.text.Format</code>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
200 extension and, as such, is used like other formatting objects (e.g.
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
201 <code>java.text.SimpleDateFormat</code>):
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
202 <div class="source"><pre>FractionFormat format = new FractionFormat(); // default format
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
203 Fraction f = new Fraction(2, 4);
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
204 String s = format.format(f); // s contains &quot;1 / 2&quot;, note the reduced fraction</pre>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
205 </div>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
206 </p>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
207 <p>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
208 To customize the formatting output, one or two
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
209 <code>java.text.NumberFormat</code> instances can be used to construct
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
210 a <code>FractionFormat</code>. These number formats control the
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
211 formatting of the numerator and denominator of the fraction:
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
212 <div class="source"><pre>NumberFormat nf = NumberFormat.getInstance(Locale.FRANCE);
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
213 // create fraction format with custom number format
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
214 // when one number format is used, both numerator and
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
215 // denominator are formatted the same
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
216 FractionFormat format = new FractionFormat(nf);
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
217 Fraction f = new Fraction(2000, 3333);
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
218 String s = format.format(c); // s contains &quot;2.000 / 3.333&quot;
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
219
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
220 NumberFormat nf2 = NumberFormat.getInstance(Locale.US);
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
221 // create fraction format with custom number formats
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
222 format = new FractionFormat(nf, nf2);
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
223 s = format.format(f); // s contains &quot;2.000 / 3,333&quot;</pre>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
224 </div>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
225 </p>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
226 <p>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
227 Formatting's inverse operation, parsing, can also be performed by
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
228 <code>FractionFormat</code>. To parse a fraction from a string,
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
229 simply call the <code>parse</code> method:
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
230 <div class="source"><pre>FractionFormat ff = new FractionFormat();
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
231 Fraction f = ff.parse(&quot;-10 / 21&quot;);</pre>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
232 </div>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
233 </p>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
234 </div>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
235 </div>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
236
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
237 </div>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
238 </div>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
239 <div class="clear">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
240 <hr/>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
241 </div>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
242 <div id="footer">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
243 <div class="xright">&#169;
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
244 2003-2010
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
245
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
246
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
247
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
248
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
249
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
250
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
251
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
252
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
253
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
254 </div>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
255 <div class="clear">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
256 <hr/>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
257 </div>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
258 </div>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
259 </body>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
260 </html>