Skip to content

Instantly share code, notes, and snippets.

@ronsims2
Created September 25, 2019 00:03
Show Gist options
  • Save ronsims2/6600085929f08d8d5e37c99387260024 to your computer and use it in GitHub Desktop.
Save ronsims2/6600085929f08d8d5e37c99387260024 to your computer and use it in GitHub Desktop.
My Math library to help my daughter with her homework.
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