Increase accuracy of our rounding schemes.

feature/default_network_editable
Kevin Serrano 8 years ago
parent 717db41d0b
commit 959038132a
No known key found for this signature in database
GPG Key ID: BF999DEFC7371BA1
  1. 29
      ui/app/components/bn-as-decimal-input.js

@ -30,8 +30,8 @@ BnAsDecimalInput.prototype.render = function () {
const suffix = props.suffix const suffix = props.suffix
const style = props.style const style = props.style
const scale = Math.pow(10, precision) const valueString = value.toString(10)
const newValue = value.toNumber(10) / scale const newValue = downsize(valueString, precision, precision)
return ( return (
h('.flex-column', [ h('.flex-column', [
@ -63,7 +63,9 @@ BnAsDecimalInput.prototype.render = function () {
onChange: (event) => { onChange: (event) => {
this.updateValidity(event) this.updateValidity(event)
const value = (event.target.value === '') ? '' : event.target.value 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) const precisionBN = new BN(scaledNumber, 10)
onChange(precisionBN) onChange(precisionBN)
}, },
@ -141,3 +143,24 @@ BnAsDecimalInput.prototype.constructWarning = function () {
return message 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
}

Loading…
Cancel
Save