Created
January 31, 2021 16:35
-
-
Save matt-dale/2fc69be8bde528c8c082d8996edd3511 to your computer and use it in GitHub Desktop.
toFixed(2) replacement. Gets around floating point division to produce a value from 0 to 1 for use as a percentage in a progress bar
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 totalLength = [] | |
totalLength.length = 10000 | |
totalLength.fill(1) | |
const values = [] | |
totalLength.forEach((x, idx) => values.push(idx)) | |
const toFixedReplacement = (val1, val2) => String(Math.min(val1/val2, 1)).substring(0,4) | |
const toFixed = (val1, val2) => Math.min(val1/val2,1).toFixed(2) | |
let toFixedResultCount = 0 | |
let toFixedReplacementResultCount = 0 | |
values.forEach((v, idx) => { | |
for (let x of values.slice(idx+1, values.length+1)){ | |
const replacement = toFixedReplacement(v,x) | |
const toFixedOriginal = toFixed(v,x) | |
if (parseFloat(replacement) >= 1.00){ | |
toFixedReplacementResultCount += 1 | |
} | |
if (parseFloat(toFixedOriginal) >= 1.00){ | |
toFixedResultCount += 1 | |
} | |
} | |
}) | |
console.assert(toFixedReplacementResultCount === 0) | |
console.assert(toFixedResultCount > 0) | |
console.log(toFixedReplacementResultCount) | |
console.log(toFixedResultCount) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment