alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Commons Math example source code file (fraction.xml)

This example Commons Math source code file (fraction.xml) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Commons Math tags/keywords

apache, fraction, fraction, fractionformat, fractionformat, license, license, of, see, the, the, these, to, you

The Commons Math fraction.xml source code

<?xml version="1.0"?>

<!--
    Licensed to the Apache Software Foundation (ASF) under one or more
    contributor license agreements.  See the NOTICE file distributed with
    this work for additional information regarding copyright ownership.
    The ASF licenses this file to You under the Apache License, Version 2.0
    (the "License"); you may not use this file except in compliance with
    the License.  You may obtain a copy of the License at
   
         http://www.apache.org/licenses/LICENSE-2.0
   
    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
  -->
  
<?xml-stylesheet type="text/xsl" href="./xdoc.xsl"?>
<!-- $Revision: 759543 $ $Date: 2009-03-28 14:24:11 -0400 (Sat, 28 Mar 2009) $ -->
<document url="stat.html">
  <properties>
    <title>The Commons Math User Guide - Fractions
  </properties>
  <body>
    <section name="9 Fractions">
      <subsection name="9.1 Overview" href="overview">
        <p>
          The fraction packages provides a fraction number type as well as
          fraction number formatting.
        </p>
      </subsection>
      <subsection name="9.2 Fraction Numbers" href="fraction">
        <p>
          <a href="../apidocs/org/apache/commons/math/fraction/Fraction.html">
          org.apache.commons.math.fraction.Fraction</a> and
          <a href="../apidocs/org/apache/commons/math/fraction/BigFraction.html">
          org.apache.commons.math.fraction.BigFraction</a> provide fraction number
          type that forms the basis for the fraction functionality found in 
          commons-math. The former one can be used for fractions whose numerators
          and denominators are small enough to fit in an int (taking care of intermediate
          values) while the second class should be used when there is a risk the numerator
          and denominator grow very large.
        </p>
        <p>
          A fraction number, can be built from two integer arguments representing numerator
          and denominator or from a double which will be approximated:
          <source>Fraction f = new Fraction(1, 3); // 1 / 3
Fraction g = new Fraction(0.25); // 1 / 4</source>
        </p>
        <p>
          Of special note with fraction construction, when a fraction is created it is always reduced to lowest terms.
        </p>
        <p>
          The <code>Fraction class provides many unary and binary
          fraction operations.  These operations provide the means to add,
          subtract, multiple and, divide fractions along with other functions similar to the real number functions found in
          <code>java.math.BigDecimal:
          <source>Fraction lhs = new Fraction(1, 3);
Fraction rhs = new Fraction(2, 5);

Fraction answer = lhs.add(rhs);     // add two fractions
        answer = lhs.subtract(rhs); // subtract two fractions
        answer = lhs.abs();         // absolute value
        answer = lhs.reciprocal();  // reciprocal of lhs</source>
        </p>
        <p>
          Like fraction construction, for each of the fraction functions, the resulting fraction is reduced to lowest terms.
        </p>
      </subsection>
      <subsection name="9.3 Fraction Formatting and Parsing" href="formatting">
        <p>
          <code>Fraction instances can be converted to and from strings
          using the<a href="../apidocs/org/apache/commons/math/fraction/FractionFormat.html">
          org.apache.commons.math.fraction.FractionFormat</a> class.
          <code>FractionFormat is a java.text.Format
          extension and, as such, is used like other formatting objects (e.g.
          <code>java.text.SimpleDateFormat):
          <source>FractionFormat format = new FractionFormat(); // default format
Fraction f = new Fraction(2, 4);
String s = format.format(f); // s contains "1 / 2", note the reduced fraction</source>
        </p>
        <p>
          To customize the formatting output, one or two
          <code>java.text.NumberFormat instances can be used to construct
          a <code>FractionFormat.  These number formats control the
          formatting of the numerator and denominator of the fraction:
          <source>NumberFormat nf = NumberFormat.getInstance(Locale.FRANCE);
// create fraction format with custom number format
// when one number format is used, both numerator and
// denominator are formatted the same
FractionFormat format = new FractionFormat(nf);
Fraction f = new Fraction(2000, 3333);
String s = format.format(c); // s contains "2.000 / 3.333"

NumberFormat nf2 = NumberFormat.getInstance(Locale.US);
// create fraction format with custom number formats
format = new FractionFormat(nf, nf2);
s = format.format(f); // s contains "2.000 / 3,333"</source>
        </p>
        <p>
          Formatting's inverse operation, parsing, can also be performed by
          <code>FractionFormat.  To parse a fraction from a string,
          simply call the <code>parse method:
          <source>FractionFormat ff = new FractionFormat();
Fraction f = ff.parse("-10 / 21");</source>
        </p>
      </subsection>
    </section>
  </body>
</document>

Other Commons Math examples (source code examples)

Here is a short list of links related to this Commons Math fraction.xml source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.