Skip to content
This repository has been archived by the owner on Jul 23, 2024. It is now read-only.

Commit

Permalink
Merge pull request #446 from jimni1222/bn
Browse files Browse the repository at this point in the history
Added logic to handle decimal points in BigNumber
  • Loading branch information
jimni1222 authored Apr 9, 2021
2 parents 52035b7 + 2831ae3 commit b1b916a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
4 changes: 4 additions & 0 deletions packages/caver-utils/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,10 @@ const toPeb = function(number, unit) {

unit = getUnitValue(unit)

// BigNumber can support decimal points but BN does not support.
// So if BigNumber type number is came as a parameter,
// use `toString` function of BigNumber to prevent error while converting BigNumber to BN.
if (utils.isBigNumber(number)) number = number.toString()
if (!utils.isBN(number) && !_.isString(number)) {
number = tryNumberToString(number)
}
Expand Down
7 changes: 7 additions & 0 deletions test/packages/caver.utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -1989,6 +1989,13 @@ describe('caver.utils.convertToPeb', () => {

expect(_.isString(converted)).to.be.true
expect(converted === expected).to.be.true

const expectedForDecimalPoints = '2500000000000000000'

const convertingDecimalPoint = caver.utils.convertToPeb(new BigNumber(2.5), 'KLAY')

expect(_.isString(convertingDecimalPoint)).to.be.true
expect(convertingDecimalPoint === expectedForDecimalPoints).to.be.true
})

it('CAVERJS-UNIT-ETC-244: should convert to peb from kKLAY', () => {
Expand Down

0 comments on commit b1b916a

Please sign in to comment.