Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 7 additions & 13 deletions packages/neuron-wallet/src/utils/shannonToCKB.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
const DECIMAL = 8
import { formatUnit, ckbDecimals } from '@ckb-lumos/bi'

const shannonToCKB = (shannon: bigint) => {
if (shannon === BigInt(0)) {
return `+0.${'0'.repeat(DECIMAL)}`
}

const isNegative = shannon < 0
const absStr = isNegative ? `${shannon}`.slice(1) : `${shannon}`
if (absStr.length <= DECIMAL) {
return `${isNegative ? '-' : '+'}0.${absStr.padStart(DECIMAL, '0')}`
}
const int = absStr.slice(0, -1 * DECIMAL)
const dec = absStr.slice(-1 * DECIMAL)
return `${isNegative ? '-' : '+'}${int}.${dec}`
return new Intl.NumberFormat('en-US', {
useGrouping: false,
signDisplay: 'always',
minimumFractionDigits: ckbDecimals,
maximumFractionDigits: ckbDecimals,
}).format(formatUnit(shannon, 'ckb') as any)
Comment thread
yanguoyu marked this conversation as resolved.
}

export default shannonToCKB
2 changes: 1 addition & 1 deletion packages/neuron-wallet/src/utils/to-csv-row.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const toCSVRow = (
txType = +tx.sudtInfo.amount <= 0 ? `UDT ${SEND_TYPE}` : `UDT ${RECEIVE_TYPE}`
}
} else {
amount = shannonToCKB(BigInt(tx.value ?? ''))
amount = shannonToCKB(BigInt(tx.value ?? '0'))
if (tx.nervosDao) {
txType = `Nervos DAO`
} else if (['create', 'destroy'].includes(tx.type || '')) {
Expand Down