Created
October 2, 2019 21:57
-
-
Save rauljose/48d6e9b0c00983ca005bc6b5f22d6170 to your computer and use it in GitHub Desktop.
percentage helper. calculate amount, total, percentage from 2 of these
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* | |
* @param initOptions object {percentage:0.16,decimals_amount:2,decimals_percent:2} defaults | |
* @returns {{calcTotal: *, calcAmount: *, options: *, calcPercent: *, option: *}} | |
* | |
* @example | |
* var percer = new iaPercenter( {percentage:0.16,decimals_amount:2,decimals_percent:2} ); | |
* | |
* percer.calcPercent(100,116) => 0.16 | |
* | |
* percer.calcTotal(100) => 116 | |
* percer.calcTotal(100, 0.2) => 120 | |
* | |
* percer.calcAmount(116) => 116 | |
* percer.calcAmount(120, 0.2) => 120 | |
* | |
* percer.options() => {percentage:0.16,decimals_amount:2,decimals_percent:2} | |
* percer.options('decimals_amount', 4) => sets deciamls_amount to 4, returns this | |
* percer.options({decimals_percent:3}) => sets decimals_percent to 3 returns this | |
* | |
* | |
* @author Raúl José Santos | |
* @author Informática Asociada | |
* @copyright 2017 | |
* @license MIT | |
* @version 1.0.0 | |
*/ | |
function iaPercenter(initOptions) { | |
if (!(this instanceof iaPercenter)) | |
return new iaPercenter(initOptions); | |
var settings = {percentage:0.16, decimals_amount:2, decimals_percent:2}; | |
if(typeof initOptions === 'object' && initOptions !== null) | |
for(var o in initOptions) | |
if(initOptions.hasOwnProperty(o)) | |
settings[o] = initOptions[o]; | |
// functionality ________________________________________ | |
function percentage(amount, total) { | |
return eq0(total) || eq0(amount) ? 0.00 : round(total/amount-1.00, settings.decimals_percent); | |
} | |
function total(amount, percentage) { | |
if(typeof percentage === 'undefined') | |
percentage = settings.percentage; | |
return round(amount*(1+percentage), settings.decimals_amount); | |
} | |
function amount(total, percentage) { | |
if(typeof percentage === 'undefined') | |
percentage = settings.percentage; | |
return eqMinus1(percentage) === -1 ? 0.00 : round(total/(1+percentage), settings.decimals_amount); | |
} | |
function opt(key,value) { | |
if(typeof key === 'undefined' || key === null) | |
return settings; | |
if(typeof key === 'object') | |
for(var o in initOptions) | |
if(initOptions.hasOwnProperty(o)) | |
settings[o] = initOptions[o]; | |
if(typeof value === 'undefined') | |
return settings[key]; | |
settings[key] = value; | |
return this; | |
} | |
// helpers ___________________________________________ | |
function eq0(n) {return round(n, settings.decimals_amount) === 0;} | |
function eqMinus1(n) {return round(n, settings.decimals_percent) === -1;} | |
function round(n, decimals) { | |
var r=parseFloat('1E'+decimals); | |
return Math.round(n*r)/r; | |
} | |
var exportThis = { | |
calcPercent: percentage, | |
calcTotal: total, | |
calcAmount: amount, | |
option: opt, | |
options: opt | |
}; | |
for(var e in exportThis) | |
if(exportThis.hasOwnProperty(e)) | |
Object.defineProperty(exportThis, e, { writable: false,configurable:false }); | |
return exportThis; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment