Skip to content

Commit 07e4370

Browse files
committed
feat(neuron-ui): display balance with thousandth delimiter
1 parent 7728c6b commit 07e4370

File tree

2 files changed

+25
-22
lines changed

2 files changed

+25
-22
lines changed

packages/neuron-ui/src/utils/formatters.test.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -144,15 +144,15 @@ describe(`formatters`, () => {
144144
},
145145
{
146146
source: `1234567890123456789012345678901234567890123456789012345678901234567890123400000000`,
147-
target: `12345678901234567890123456789012345678901234567890123456789012345678901234`,
147+
target: `12,345,678,901,234,567,890,123,456,789,012,345,678,901,234,567,890,123,456,789,012,345,678,901,234`,
148148
},
149149
{
150150
source: `12345678901234567890123456789012345678901234567890123456789012345678901234`,
151-
target: `123456789012345678901234567890123456789012345678901234567890123456.78901234`,
151+
target: `123,456,789,012,345,678,901,234,567,890,123,456,789,012,345,678,901,234,567,890,123,456.78901234`,
152152
},
153153
{
154154
source: `1234567890123456789012345678901234567890123456789012345678901234567890123400`,
155-
target: `12345678901234567890123456789012345678901234567890123456789012345678.901234`,
155+
target: `12,345,678,901,234,567,890,123,456,789,012,345,678,901,234,567,890,123,456,789,012,345,678.901234`,
156156
},
157157

158158
{
@@ -173,15 +173,15 @@ describe(`formatters`, () => {
173173
},
174174
{
175175
source: `-1234567890123456789012345678901234567890123456789012345678901234567890123400000000`,
176-
target: `-12345678901234567890123456789012345678901234567890123456789012345678901234`,
176+
target: `-12,345,678,901,234,567,890,123,456,789,012,345,678,901,234,567,890,123,456,789,012,345,678,901,234`,
177177
},
178178
{
179179
source: `-12345678901234567890123456789012345678901234567890123456789012345678901234`,
180-
target: `-123456789012345678901234567890123456789012345678901234567890123456.78901234`,
180+
target: `-123,456,789,012,345,678,901,234,567,890,123,456,789,012,345,678,901,234,567,890,123,456.78901234`,
181181
},
182182
{
183183
source: `-1234567890123456789012345678901234567890123456789012345678901234567890123400`,
184-
target: `-12345678901234567890123456789012345678901234567890123456789012345678.901234`,
184+
target: `-12,345,678,901,234,567,890,123,456,789,012,345,678,901,234,567,890,123,456,789,012,345,678.901234`,
185185
},
186186
{
187187
source: `0`,

packages/neuron-ui/src/utils/formatters.ts

+19-16
Original file line numberDiff line numberDiff line change
@@ -83,23 +83,26 @@ export const CKBToShannonFormatter = (amount: string, uint: CapacityUnit) => {
8383

8484
export const shannonToCKBFormatter = (shannon: string) => {
8585
const sign = shannon.startsWith('-') ? '-' : ''
86-
const unsignedShannon = shannon.replace(/^-/, '')
87-
let unsignedCkbStr = [...unsignedShannon.padStart(8, '0')]
88-
.map((char, idx) => {
89-
if (idx === Math.max(unsignedShannon.length - 8, 0)) {
90-
return `.${char}`
91-
}
92-
return char
93-
})
94-
.join('')
95-
.replace(/(^0+|\.?0+$)/g, '')
96-
if (!unsignedCkbStr) {
97-
return '0'
86+
const unsignedShannon = shannon.replace(/^-?0*/, '')
87+
let unsignedCKB = ''
88+
if (unsignedShannon.length <= 8) {
89+
unsignedCKB = `0.${unsignedShannon.padStart(8, '0')}`.replace(/\.?0+$/, '')
90+
} else {
91+
const decimal = `.${unsignedShannon.slice(-8)}`.replace(/\.?0+$/, '')
92+
const int = unsignedShannon.slice(0, -8).replace(/\^0+/, '')
93+
unsignedCKB = `${(
94+
int
95+
.split('')
96+
.reverse()
97+
.join('')
98+
.match(/\d{1,3}/g) || ['0']
99+
)
100+
.join(',')
101+
.split('')
102+
.reverse()
103+
.join('')}${decimal}`
98104
}
99-
if (unsignedCkbStr.startsWith('.')) {
100-
unsignedCkbStr = `0${unsignedCkbStr}`
101-
}
102-
return sign + unsignedCkbStr
105+
return +unsignedCKB === 0 ? '0' : `${sign}${unsignedCKB}`
103106
}
104107

105108
export const localNumberFormatter = (num: string | number = 0) => {

0 commit comments

Comments
 (0)