README
math-lib
Description
An extension of the Number prototype chain to add some more functions to the Number object. More information about what this extension adds can be found in the API section.
API
Table of Contents
- round
- floor
- ceil
- pad
- degToRad
- radToDeg
- toDollars
- tax
- withTax
- interest
- mortage
- decimalToHex
- GOLDENRATIO
round
Round a number to it's nearest whole number
Returns Number the closest whole number to the original number
floor
Round a number down to it's nearest whole number.
Returns Number the closest whole number to the rounded down original number.
ceil
Round a number up to it's closest whole number
Examples
Number(5.32).ceil() -> 6
Returns Number the closest whole number to the rounded up original number.
pad
Pad a number with a specified amount of zeros (On either the left or right side)
Parameters
leftPadding
rightPadding
Examples
Number(5.3234).pad(4,5) -> 0004.32340
Number(4.34).pad(2,0) -> 04.34
Number(23.32).pad(10,10) -> 0000000023.3200000000
Returns String A padded string that indicates.
degToRad
Convert a number from degrees to radians
Examples
Number(20).degToRad() -> 0.349066
Returns Number The converted number in radians
radToDeg
Convert a number from radians to degrees
Examples
Number(1).radToDeg() -> 57.2958
Returns Number the converted number in degrees.
toDollars
Convert a number to USD.
Examples
Number(1).toDollars() -> $1.00
Number(1.05).toDollars() -> $1.05
Number(0.5).toDollars() -> ยข0.5
Returns String The dollar representation of the number
tax
Return the tax amount from the number that would be applied given the rate
Parameters
rate
Number The tax rate to be applied where the rate is 0 <= rate <= 100 or the rate is 0 <= 100
Examples
Number(10).tax(10) -> 1.00
Number(60).tax(25) -> 15.00
Returns Number the total tax amount of the current number multiplied by the rate
withTax
Return this number with a specified tax rate applied to it.
Parameters
rate
Number The tax rate to be applied
Examples
Number(10).withTax(10) -> 11
Number(200).withTax(50) -> 300
Returns Number a new number that has the tax of the original number applied.
interest
Calculate the interest on a number given the interest rate, years, and optional decimal places to round to
Parameters
rate
Number The interest rateyears
Number The amount of years the rate is applied to return {String} a string containing the total interest paid on a given numberdecimalPlaces
(optional, default2
)
mortage
Calculate the monthly payments for a mortage given a principal (Number)
Parameters
interestRate
Number The yearly rate of interestnumberOfYears
Number the years remaining for the mortage
Returns Number The monthly mortage payments that need to be made to pay off the mortage
decimalToHex
convert a decimal number to hexadecimal, all thanks to the help of this stack overflow question: https://stackoverflow.com/questions/57803/how-to-convert-decimal-to-hexadecimal-in-javascript
Returns String The string containing the number converted to hexadecimal
GOLDENRATIO
The golden ratio as approximated by: https://en.wikipedia.org/wiki/Golden_ratio
Type: Number