Browse Source

switched math rounding with display rounding

all-modes
pbca26 7 years ago
parent
commit
2e6337eaab
  1. 8
      react/src/components/dashboard/walletsBalance/walletsBalance.render.js
  2. 10
      react/src/components/dashboard/walletsData/walletsData.js
  3. 2
      react/src/components/dashboard/walletsData/walletsData.render.js
  4. 66
      react/src/util/formatValue.js

8
react/src/components/dashboard/walletsBalance/walletsBalance.render.js

@ -38,7 +38,7 @@ const WalletsBalanceRender = function() {
<span
className="pull-right padding-top-10 font-size-22"
title={ this.renderBalance('transparent') }>
{ Config.roundValues ? formatValue('round', this.renderBalance('transparent'), -6) : this.renderBalance('transparent') }
{ Config.roundValues ? formatValue(this.renderBalance('transparent')) : this.renderBalance('transparent') }
</span>
</div>
</div>
@ -58,7 +58,7 @@ const WalletsBalanceRender = function() {
<span
className="pull-right padding-top-10 font-size-22"
title={ this.renderBalance('private') }>
{ Config.roundValues ? formatValue('round', this.renderBalance('private'), -6) : this.renderBalance('private') }
{ Config.roundValues ? formatValue(this.renderBalance('private')) : this.renderBalance('private') }
</span>
</div>
</div>
@ -79,7 +79,7 @@ const WalletsBalanceRender = function() {
<span
className="pull-right padding-top-10 font-size-22"
title={ this.renderBalance('interest') }>
{ Config.roundValues ? formatValue('round', this.renderBalance('interest'), -6) : this.renderBalance('interest') }
{ Config.roundValues ? formatValue(this.renderBalance('interest')) : this.renderBalance('interest') }
</span>
</div>
</div>
@ -100,7 +100,7 @@ const WalletsBalanceRender = function() {
<span
className="pull-right padding-top-10 font-size-22"
title={ this.renderBalance('total') }>
{ Config.roundValues ? formatValue('round', this.renderBalance('total'), -6) : this.renderBalance('total') }
{ Config.roundValues ? formatValue(this.renderBalance('total')) : this.renderBalance('total') }
</span>
</div>
</div>

10
react/src/components/dashboard/walletsData/walletsData.js

@ -385,7 +385,7 @@ class WalletsData extends React.Component {
itemsList: this.props.ActiveCoin.txhistory,
filteredItemsList: this.filterTransactions(this.props.ActiveCoin.txhistory, this.state.searchTerm),
txhistory: this.props.ActiveCoin.txhistory,
showPagination: this.props.ActiveCoin.txhistory && this.props.ActiveCoin.txhistory.length >= this.state.defaultPageSize
showPagination: this.props.ActiveCoin.txhistory && this.props.ActiveCoin.txhistory.length >= this.state.defaultPageSize,
}));
}
}
@ -402,7 +402,7 @@ class WalletsData extends React.Component {
}
this.setState({
itemsListColumns: this.generateItemsListColumns()
itemsListColumns: this.generateItemsListColumns(),
});
}
@ -509,7 +509,7 @@ class WalletsData extends React.Component {
}
if (_amount !== 'N/A') {
_amount = formatValue('round', _amount, -6);
_amount = formatValue(_amount);
}
items.push(
@ -546,7 +546,7 @@ class WalletsData extends React.Component {
let _amount = _addresses.public[i].amount;
if (_amount !== 'N/A') {
_amount = formatValue('round', _amount, -6);
_amount = formatValue(_amount);
}
return _amount;
@ -561,7 +561,7 @@ class WalletsData extends React.Component {
}
if (_amount !== 'N/A') {
_amount = formatValue('round', _amount, -6);
_amount = formatValue(_amount);
}
return _amount;

2
react/src/components/dashboard/walletsData/walletsData.render.js

@ -155,7 +155,7 @@ export const TxAmountRender = function(tx) {
if (Config.roundValues) {
return (
<span title={ tx.amount * _amountNegative }>{ formatValue('round', tx.amount, -6) * _amountNegative || translate('DASHBOARD.UNKNOWN') }</span>
<span title={ tx.amount * _amountNegative }>{ formatValue(tx.amount) * _amountNegative || translate('DASHBOARD.UNKNOWN') }</span>
);
}

66
react/src/util/formatValue.js

@ -1,52 +1,24 @@
// ref: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Math/round#Decimal_rounding
export function formatValue(formatType, formatValue, formatExp) {
let _formatExp;
/**
* Decimal adjustment of a number.
*
* @param {String} type The type of adjustment.
* @param {Number} value The number.
* @param {Integer} exp The exponent (the 10 logarithm of the adjustment base).
* @returns {Number} The adjusted value.
*/
function decimalAdjust(type, value, exp) {
// If the exp is undefined or zero...
if (typeof exp === 'undefined' || +exp === 0) {
return Math[type](value);
}
value = +value;
exp = +exp;
// If the value is not a number or the exp is not an integer...
if (isNaN(value) || !(typeof exp === 'number' && exp % 1 === 0)) {
return NaN;
}
// If the value is negative...
if (value < 0) {
return -decimalAdjust(type, -value, exp);
}
// Shift
value = value.toString().split('e');
value = Math[type](+(value[0] + 'e' + (value[1] ? (+value[1] - exp) : -exp)));
// Shift back
value = value.toString().split('e');
return +(value[0] + 'e' + (value[1] ? (+value[1] + exp) : exp));
}
// display rounding
export function formatValue(formatValue) {
const _valueToStr = formatValue.toString();
if (Math.abs(Number(formatValue)) >= 1) {
_formatExp = -3;
if (_valueToStr.indexOf('.') === -1) {
return formatValue;
} else {
_formatExp = formatExp;
}
if (_valueToStr) {
const _decimal = _valueToStr.substr(_valueToStr.indexOf('.') + 1, _valueToStr.length);
let newVal = _valueToStr.substr(0, _valueToStr.indexOf('.') + 1);
for (let i = 0; i < _decimal.length; i++) {
if (_decimal[i] === '0') {
newVal = newVal + _decimal[i];
} else {
newVal = newVal + _decimal[i];
break;
}
}
switch (formatType) {
case 'round':
return decimalAdjust('round', formatValue, _formatExp);
break;
case 'floor':
return decimalAdjust('floor', formatValue, _formatExp);
break;
case 'ceil':
return decimalAdjust('ceil', formatValue, _formatExp);
break;
return newVal;
}
}
}
Loading…
Cancel
Save