Class Calc

java.lang.Object
writer2latex.util.Calc

public class Calc extends Object
A collection of static methods used to perform calculations on strings representing floating point numbers with units or percentages. In the JavaDoc, a length refers to a string like e.g. "21.7cm" and percent refers to a string like e.g. "2.5%".
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static String
    abs(String sLength)
    Get the absolute value of a length (e.g. "-2.5cm" returns "2.5cm")
    static final String
    add(String sLength1, String sLength2)
    Add two lengths (e.g. "2.5cm" added to "1.08cm" returns "3.58cm")
    static final String
    divide(String sDividend, String sDivisor)
    Divide dividend by divisor and return the quotient as an integer percentage (e.g. "0.5cm" divided by "2cm" returns "25%").
    static final String
    divide(String sDividend, String sDivisor, boolean bMax100)
    Divide dividend by divisor and return the quotient as an integer percentage (e.g. "0.5cm" divided by "2cm" returns "25%").
    static final float
    getFloat(String sFloat, float fDefault)
    Get a float value from a string (e.g. "218.86" returns 218.86F)
    static boolean
    isLessThan(String sThis, String sThat)
    Test whether a given length is smaller than another length (e.g. "2.5cm" compared to "2.6cm" returns true;
    static boolean
    isZero(String sValue)
    Checks whether a given length is zero within a tolerance of 0.001 (e.g. "0.0005cm" returns true)
    static final String
    length2px(String sLength)
    Convert a length to px assuming 96ppi; cf. the CSS spec (e.g. "0.1in" returns "9.6px").
    static final String
    multiply(String sPercent, String sLength)
    Multiply a length by a percentage (e.g. "150%" multiplied with "2.5mm" returns "3.75cm")
    static final String
    sub(String sLength1, String sLength2)
    Subtract two lengths (e.g. "2.5cm" subtracted by "1.08cm" returns "1.42cm")
    static String
    Replace the unit inch with in on a length (e.g. "17.5inch" returns "17.5in")

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • Calc

      public Calc()
  • Method Details

    • getFloat

      public static final float getFloat(String sFloat, float fDefault)
      Get a float value from a string (e.g. "218.86" returns 218.86F)
      Parameters:
      sFloat - the string to parse
      fDefault - a default value to return if the string cannot be parsed as a float
      Returns:
      the float value of the string
    • truncateLength

      public static String truncateLength(String sValue)
      Replace the unit inch with in on a length (e.g. "17.5inch" returns "17.5in")
      Parameters:
      sValue - the length
      Returns:
      the truncated length
    • isZero

      public static boolean isZero(String sValue)
      Checks whether a given length is zero within a tolerance of 0.001 (e.g. "0.0005cm" returns true)
      Parameters:
      sValue - the length to check
      Returns:
      true if the value is close to zero
    • length2px

      public static final String length2px(String sLength)
      Convert a length to px assuming 96ppi; cf. the CSS spec (e.g. "0.1in" returns "9.6px"). Exception: Never return less than 1px
      Parameters:
      sLength - the length to convert
      Returns:
      the converted length
    • divide

      public static final String divide(String sDividend, String sDivisor)
      Divide dividend by divisor and return the quotient as an integer percentage (e.g. "0.5cm" divided by "2cm" returns "25%"). Exception: Never returns below 1% except if the dividend is zero.
      Parameters:
      sDividend - the length to use as dividend
      sDivisor - the length to use as divisor
      Returns:
      the quotient percentage
    • divide

      public static final String divide(String sDividend, String sDivisor, boolean bMax100)
      Divide dividend by divisor and return the quotient as an integer percentage (e.g. "0.5cm" divided by "2cm" returns "25%"). Exception: Never returns below 1% except if the dividend is zero, and never returns above 100% if last parameter is true.
      Parameters:
      sDividend - the length to use as dividend
      sDivisor - the length to use as divisor
      bMax100 - true if a maximum of 100% should be returned
      Returns:
      the quotient percentage
    • multiply

      public static final String multiply(String sPercent, String sLength)
      Multiply a length by a percentage (e.g. "150%" multiplied with "2.5mm" returns "3.75cm")
      Parameters:
      sPercent - the percentage
      sLength - the length
      Returns:
      the product length
    • add

      public static final String add(String sLength1, String sLength2)
      Add two lengths (e.g. "2.5cm" added to "1.08cm" returns "3.58cm")
      Parameters:
      sLength1 - the first length term
      sLength2 - the second length term
      Returns:
      the sum (as a length with the same unit as the first term)
    • sub

      public static final String sub(String sLength1, String sLength2)
      Subtract two lengths (e.g. "2.5cm" subtracted by "1.08cm" returns "1.42cm")
      Parameters:
      sLength1 - the first length term
      sLength2 - the second length term
      Returns:
      the difference (as a length with the same unit as the first term)
    • isLessThan

      public static boolean isLessThan(String sThis, String sThat)
      Test whether a given length is smaller than another length (e.g. "2.5cm" compared to "2.6cm" returns true;
      Parameters:
      sThis - is this length the smaller?
      sThat - is this length the larger?
      Returns:
      true is the first length is smaller than the second length
    • abs

      public static String abs(String sLength)
      Get the absolute value of a length (e.g. "-2.5cm" returns "2.5cm")
      Parameters:
      sLength - the length
      Returns:
      the absolute value