diff options
author | Kevin Serrano <kevin.serrano@consensys.net> | 2017-05-24 01:43:37 +0800 |
---|---|---|
committer | Kevin Serrano <kevin.serrano@consensys.net> | 2017-05-24 01:43:37 +0800 |
commit | 959038132a6780f1dd7a4db3696d3fdbaad83b88 (patch) | |
tree | 656454d1cc9fab767bd92fe59d64d7071147fdf5 /ui/app/components/bn-as-decimal-input.js | |
parent | 717db41d0b7bcd7b6f88a5c460aa4dcbc5828116 (diff) | |
download | tangerine-wallet-browser-959038132a6780f1dd7a4db3696d3fdbaad83b88.tar tangerine-wallet-browser-959038132a6780f1dd7a4db3696d3fdbaad83b88.tar.gz tangerine-wallet-browser-959038132a6780f1dd7a4db3696d3fdbaad83b88.tar.bz2 tangerine-wallet-browser-959038132a6780f1dd7a4db3696d3fdbaad83b88.tar.lz tangerine-wallet-browser-959038132a6780f1dd7a4db3696d3fdbaad83b88.tar.xz tangerine-wallet-browser-959038132a6780f1dd7a4db3696d3fdbaad83b88.tar.zst tangerine-wallet-browser-959038132a6780f1dd7a4db3696d3fdbaad83b88.zip |
Increase accuracy of our rounding schemes.
Diffstat (limited to 'ui/app/components/bn-as-decimal-input.js')
-rw-r--r-- | ui/app/components/bn-as-decimal-input.js | 29 |
1 files changed, 26 insertions, 3 deletions
diff --git a/ui/app/components/bn-as-decimal-input.js b/ui/app/components/bn-as-decimal-input.js index d0eebe09e..fbe36abfb 100644 --- a/ui/app/components/bn-as-decimal-input.js +++ b/ui/app/components/bn-as-decimal-input.js @@ -30,8 +30,8 @@ BnAsDecimalInput.prototype.render = function () { const suffix = props.suffix const style = props.style - const scale = Math.pow(10, precision) - const newValue = value.toNumber(10) / scale + const valueString = value.toString(10) + const newValue = downsize(valueString, precision, precision) return ( h('.flex-column', [ @@ -63,7 +63,9 @@ BnAsDecimalInput.prototype.render = function () { onChange: (event) => { this.updateValidity(event) const value = (event.target.value === '') ? '' : event.target.value - const scaledNumber = Math.floor(scale * value) + + + const scaledNumber = upsize(value, precision, precision) const precisionBN = new BN(scaledNumber, 10) onChange(precisionBN) }, @@ -141,3 +143,24 @@ BnAsDecimalInput.prototype.constructWarning = function () { return message } + + +function downsize (number, scale, precision) { + if (scale === 0) { + return Number(number) + } else { + var decimals = (scale === precision) ? -1 : scale - precision + return Number(number.slice(0, -scale) + '.' + number.slice(-scale, decimals)) + } +} + +function upsize (number, scale, precision) { + var string = number.toString() + var stringArray = string.split('.') + var decimalLength = stringArray[1] ? stringArray[1].length : 0 + var newString = ((scale === 0) || (decimalLength === 0)) ? stringArray[0] : stringArray[0] + stringArray[1].slice(0, precision) + for (var i = decimalLength; i < scale; i++) { + newString += '0' + } + return newString +} |