Skip to content

Commit 4dfa141

Browse files
authored
Merge pull request #1033 from alexstotsky/improve-numb-format-precision
(improvement) Numbers formaters precision
2 parents 619519a + ab0364f commit 4dfa141

3 files changed

Lines changed: 21 additions & 6 deletions

File tree

package-lock.json

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"@blueprintjs/table": "3.5.0",
2525
"@blueprintjs/timezone": "3.10.1",
2626
"axios": "^1.8.4",
27+
"bignumber.js": "^9.3.1",
2728
"classnames": "2.5.1",
2829
"connected-react-router": "6.9.3",
2930
"flexboxgrid2": "7.2.1",

src/ui/utils.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React from 'react'
22
import classNames from 'classnames'
3-
import _round from 'lodash/round'
3+
import BigNumber from 'bignumber.js'
44

55
export const amountStyle = (amount) => {
66
const val = parseFloat(amount)
@@ -26,7 +26,11 @@ export const insertIf = (condition, ...elements) => (condition ? elements : [])
2626

2727
export const filterSelectorItem = (query, item) => item.toLowerCase().indexOf(query.toLowerCase()) >= 0
2828

29-
export const fixedFloat = (val, num = 8) => (typeof val === 'number' ? val && val.toFixed(num) : val)
29+
export const fixedFloat = (val, num = 8) => {
30+
if (!val && val !== 0) return val
31+
const bn = new BigNumber(val)
32+
return bn.isNaN() ? val : bn.toFixed(num)
33+
}
3034

3135
export const formatThousands = (value) => {
3236
const parts = value.toString().split('.')
@@ -71,11 +75,12 @@ export const formatAmount = (val, options = {}) => {
7175
formatThousands: shouldFormatThousands = false,
7276
dollarSign = false,
7377
} = options
74-
let roundedValue = _round(val, 8)
78+
const bnValue = new BigNumber(val)
79+
let roundedValue = bnValue.dp(8, BigNumber.ROUND_HALF_UP).toFixed()
7580

7681
const classes = classNames('bitfinex-amount', {
77-
'bitfinex-green-text': color ? color === 'green' : roundedValue > 0,
78-
'bitfinex-red-text': color ? color === 'red' : roundedValue < 0,
82+
'bitfinex-green-text': color ? color === 'green' : bnValue.gt(0),
83+
'bitfinex-red-text': color ? color === 'red' : bnValue.lt(0),
7984
})
8085

8186
if (fixFraction) {
@@ -106,7 +111,7 @@ export const formatAmount = (val, options = {}) => {
106111
)
107112
}
108113

109-
export const formatFee = (fee) => formatAmount(fee * 100, { color: 'white', minDigits: 2 })
114+
export const formatFee = (fee) => formatAmount(new BigNumber(fee).times(100), { color: 'white', minDigits: 2 })
110115

111116
export const formatColor = (value, color) => {
112117
const classes = classNames({

0 commit comments

Comments
 (0)