Cleanup code.

feature/default_network_editable
Kevin Serrano 8 years ago
parent 31d17c9e25
commit 60281f7250
No known key found for this signature in database
GPG Key ID: BF999DEFC7371BA1
  1. 22
      ui/app/components/bn-as-decimal-input.js

@ -31,7 +31,7 @@ BnAsDecimalInput.prototype.render = function () {
const suffix = props.suffix const suffix = props.suffix
const style = props.style const style = props.style
const valueString = value.toString(10) const valueString = value.toString(10)
const newValue = downsize(valueString, scale, precision) const newValue = this.downsize(valueString, scale, precision)
return ( return (
h('.flex-column', [ h('.flex-column', [
@ -65,7 +65,7 @@ BnAsDecimalInput.prototype.render = function () {
const value = (event.target.value === '') ? '' : event.target.value const value = (event.target.value === '') ? '' : event.target.value
const scaledNumber = upsize(value, scale, precision) const scaledNumber = this.upsize(value, scale, precision)
const precisionBN = new BN(scaledNumber, 10) const precisionBN = new BN(scaledNumber, 10)
onChange(precisionBN) onChange(precisionBN)
}, },
@ -145,20 +145,28 @@ BnAsDecimalInput.prototype.constructWarning = function () {
} }
function downsize (number, scale, precision) { BnAsDecimalInput.prototype.downsize = function (number, scale, precision) {
// if there is no scaling, simply return the number
if (scale === 0) { if (scale === 0) {
return Number(number) return Number(number)
} else { } else {
// if the scale is the same as the precision, account for this edge case.
var decimals = (scale === precision) ? -1 : scale - precision var decimals = (scale === precision) ? -1 : scale - precision
return Number(number.slice(0, -scale) + '.' + number.slice(-scale, decimals)) return Number(number.slice(0, -scale) + '.' + number.slice(-scale, decimals))
} }
} }
function upsize (number, scale, precision) { BnAsDecimalInput.prototype.upsize = function (number, scale, precision) {
var string = number.toString() var stringArray = number.toString().split('.')
var stringArray = string.split('.')
var decimalLength = stringArray[1] ? stringArray[1].length : 0 var decimalLength = stringArray[1] ? stringArray[1].length : 0
var newString = ((scale === 0) || (decimalLength === 0)) ? stringArray[0] : stringArray[0] + stringArray[1].slice(0, precision) var newString = stringArray[0]
// If there is scaling and decimal parts exist, integrate them in.
if ((scale !== 0) && (decimalLength !== 0)) {
newString += stringArray[1].slice(0, precision)
}
// Add 0s to account for the upscaling.
for (var i = decimalLength; i < scale; i++) { for (var i = decimalLength; i < scale; i++) {
newString += '0' newString += '0'
} }

Loading…
Cancel
Save