Package writer2latex.util
Class Calc
java.lang.Object
writer2latex.util.Calc
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 -
Method Summary
Modifier and TypeMethodDescriptionstatic StringGet the absolute value of a length (e.g. "-2.5cm" returns "2.5cm")static final StringAdd two lengths (e.g. "2.5cm" added to "1.08cm" returns "3.58cm")static final StringDivide dividend by divisor and return the quotient as an integer percentage (e.g. "0.5cm" divided by "2cm" returns "25%").static final StringDivide dividend by divisor and return the quotient as an integer percentage (e.g. "0.5cm" divided by "2cm" returns "25%").static final floatGet a float value from a string (e.g. "218.86" returns 218.86F)static booleanisLessThan(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 booleanChecks whether a given length is zero within a tolerance of 0.001 (e.g. "0.0005cm" returns true)static final StringConvert a length to px assuming 96ppi; cf. the CSS spec (e.g. "0.1in" returns "9.6px").static final StringMultiply a length by a percentage (e.g. "150%" multiplied with "2.5mm" returns "3.75cm")static final StringSubtract two lengths (e.g. "2.5cm" subtracted by "1.08cm" returns "1.42cm")static StringtruncateLength(String sValue) Replace the unit inch with in on a length (e.g. "17.5inch" returns "17.5in")
-
Constructor Details
-
Calc
public Calc()
-
-
Method Details
-
getFloat
Get a float value from a string (e.g. "218.86" returns 218.86F)- Parameters:
sFloat- the string to parsefDefault- a default value to return if the string cannot be parsed as a float- Returns:
- the float value of the string
-
truncateLength
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
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
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
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 dividendsDivisor- the length to use as divisor- Returns:
- the quotient percentage
-
divide
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 dividendsDivisor- the length to use as divisorbMax100- true if a maximum of 100% should be returned- Returns:
- the quotient percentage
-
multiply
Multiply a length by a percentage (e.g. "150%" multiplied with "2.5mm" returns "3.75cm")- Parameters:
sPercent- the percentagesLength- the length- Returns:
- the product length
-
add
Add two lengths (e.g. "2.5cm" added to "1.08cm" returns "3.58cm")- Parameters:
sLength1- the first length termsLength2- the second length term- Returns:
- the sum (as a length with the same unit as the first term)
-
sub
Subtract two lengths (e.g. "2.5cm" subtracted by "1.08cm" returns "1.42cm")- Parameters:
sLength1- the first length termsLength2- the second length term- Returns:
- the difference (as a length with the same unit as the first term)
-
isLessThan
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
Get the absolute value of a length (e.g. "-2.5cm" returns "2.5cm")- Parameters:
sLength- the length- Returns:
- the absolute value
-