Created
September 27, 2024 23:46
-
-
Save alshdavid/00ac6a35ec8e03ec65cb5a604fac7de4 to your computer and use it in GitHub Desktop.
Calculate tax from brackets
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
const MAX = Number.MAX_SAFE_INTEGER | |
const BRACKETS = { | |
AU_2025: [ | |
[ 0, 18200, 0.00 ], | |
[ 18201, 45000, 0.16 ], | |
[ 45001, 135000, 0.30 ], | |
[ 135001, 190000, 0.37 ], | |
[ 190001, MAX, 0.45 ], | |
], | |
AU_2024: [ | |
[ 0, 18200, 0.00 ], | |
[ 18201, 45000, 0.19 ], | |
[ 45001, 120000, 0.325], | |
[ 120001, 180000, 0.37 ], | |
[ 180001, MAX, 0.45 ], | |
] | |
}; | |
function calculate_tax( | |
income, | |
brackets = BRACKETS.AU_2025, | |
) { | |
let current = 0 | |
let remainingIncome = income | |
for (const [ bracketStart, bracketEnd, taxRatePercent, additionalCharge = 0 ] of brackets) { | |
const bracketSize = bracketEnd - bracketStart | |
const overflow = remainingIncome - bracketSize | |
if (overflow >= 0) { | |
current += bracketSize * taxRatePercent | |
current += additionalCharge | |
remainingIncome = overflow | |
} else { | |
current += remainingIncome * taxRatePercent | |
current += additionalCharge | |
break | |
} | |
remainingIncome = overflow | |
} | |
return parseInt(current.toFixed(0), 10) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment