Created
September 25, 2019 00:03
-
-
Save ronsims2/6600085929f08d8d5e37c99387260024 to your computer and use it in GitHub Desktop.
My Math library to help my daughter with her homework.
This file contains hidden or 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
export const roundWholeNumber = (_num, _places) => { | |
const num = String(_num) | |
const places = String(_places) | |
if (isNaN(parseInt(num)) || isNaN(parseInt(places))) { | |
return | |
} | |
if (places.length > num.length) { | |
return | |
} | |
const padSize = places.length - 1 | |
const roundDeciderIndex = num.length - padSize | |
const roundDecider = num[roundDeciderIndex] | |
const sliceSize = num.length - padSize > 0 ? num.length - padSize : 1 | |
const roundee = parseInt(num.substr(0, sliceSize)) | |
const rounded = parseInt(roundDecider) > 5 ? roundee + 1 : roundee | |
const padding = Array(padSize).fill('0').join('') | |
return `${rounded}${padding}` | |
} | |
export const truncateToPlace = (_num) => { | |
const num = parseInt(_num) | |
if (isNaN(num)) { | |
return | |
} | |
return `1${Array(String(num).length).fill('0').join('')}` | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment