Example usage for org.apache.commons.lang3.math Fraction getFraction

List of usage examples for org.apache.commons.lang3.math Fraction getFraction

Introduction

In this page you can find the example usage for org.apache.commons.lang3.math Fraction getFraction.

Prototype

public static Fraction getFraction(String str) 

Source Link

Document

Creates a Fraction from a String.

The formats accepted are:

  1. double String containing a dot
  2. 'X Y/Z'
  3. 'Y/Z'
  4. 'X' (a simple whole number)

and a .

Usage

From source file:gmgen.plugin.PlayerCharacterOutput.java

/**
 * TODO Much of this code is repeated in CRToken, Race, Combatant and PlayerCharacterOutput
 *  /*from ww  w  . j  a va  2s  .c  o m*/
 * @return An output version of the CR
 */
public String getCR() {
    Integer calcCR = display.calcCR();
    float cr = (calcCR == null) ? -1 : calcCR;
    String retString = "";

    // If the CR is a fractional CR then we convert to a 1/x format
    if ((cr > 0) && (cr < 1)) {
        Fraction fraction = Fraction.getFraction(cr); // new Fraction(CR);
        int denominator = fraction.getDenominator();
        int numerator = fraction.getNumerator();
        retString = numerator + "/" + denominator;
    } else if ((cr >= 1) || (cr == 0)) {
        int newCr = -99;
        String crAsString = Float.toString(cr);
        String decimalPlaceValue = crAsString.substring(crAsString.length() - 2);
        if (decimalPlaceValue.equals(".0")) {
            newCr = (int) cr;
        }

        retString += ((newCr > -99) ? newCr : cr);
    }
    return retString;
}

From source file:org.eclipse.tracecompass.tmf.ui.markers.PeriodicMarkerEventSource.java

private PeriodicMarkerEventSource(String category, Reference reference, double period, long rollover,
        boolean foreground, RGBA evenColor, @Nullable RGBA oddColor) {
    if (period <= 0) {
        throw new IllegalArgumentException("period cannot be less than or equal to zero"); //$NON-NLS-1$
    }//from   w  ww . ja  v a 2 s  . c o m
    if (rollover < 0) {
        throw new IllegalArgumentException("rollover cannot be less than zero"); //$NON-NLS-1$
    }
    fCategory = category;
    fReference = reference;
    fPeriod = period;
    fPeriodInteger = (long) period;
    try {
        fPeriodFraction = Fraction.getFraction(fPeriod - fPeriodInteger);
    } catch (ArithmeticException e) {
        /* can't convert to fraction, use floating-point arithmetic */
        fPeriodFraction = null;
    }
    fRollover = rollover;
    fColor = evenColor;
    fOddColor = oddColor;
    fForeground = foreground;
}

From source file:org.zkoss.ganttz.util.Interval.java

private Fraction inTheDayIncrement(DateTime date) {
    DateTime atStartOfDay = date.toLocalDate().toDateTimeAtStartOfDay();
    Duration duration = new Duration(atStartOfDay, date);
    double result = ((double) duration.getMillis()) / lengthBetween.getMillis();

    return Fraction.getFraction(result);
}

From source file:plugin.initiative.XMLCombatant.java

/**
 * //w w w .  ja v  a  2 s  .  c  o m
 * TODO Much of this code is repeated in CRToken, Race, XMLCombatant and PlayerCharacterOutput
 * 
 * <p>Gets the CR value for the character for output</p>
 * @return CR value
 */
public String getCRForOutput() {
    String retString = "";
    String crAsString = Float.toString(cr);
    String decimalPlaceValue = crAsString.substring(crAsString.length() - 2);

    // If the CR is a fractional CR then we convert to a 1/x format
    if (cr > 0 && cr < 1) {
        Fraction fraction = Fraction.getFraction(cr); // new Fraction(CR);
        int denominator = fraction.getDenominator();
        int numerator = fraction.getNumerator();
        retString = numerator + "/" + denominator;
    } else if (cr >= 1 || cr == 0) {
        int newCr = -99;
        if (decimalPlaceValue.equals(".0")) {
            newCr = (int) cr;
        }

        if (newCr > -99) {
            retString = retString + newCr;
        } else {
            retString = retString + cr;
        }
    }
    return retString;
}