Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 1 addition & 1 deletion .changeset/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
"access": "restricted",
"baseBranch": "main",
"updateInternalDependencies": "patch",
"ignore": []
"ignore": ["docs"]
}
5 changes: 5 additions & 0 deletions .changeset/eighty-friends-film.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"vn-number": patch
---

Simplified `readVnNumber` string conversion for improved readability
5 changes: 5 additions & 0 deletions .changeset/tired-years-wink.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"vn-number": patch
---

Updated logic to repeat "tỷ" based on group position dynamically, ensuring accurate representation for billions, octillions, and undecillions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"vitest": "3.2.4"
},
"workspaces": [
".",
"docs"
],
"files": [
Expand Down
5 changes: 3 additions & 2 deletions src/read/groups.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@ function getUnitSuffix(
const needsBillionSuffix = positionFromRight >= 3 && hasTrailingZeros

if (type === 3) {
// Billion: double "tỷ" for the second billion cycle (position >= 6)
// Billion: repeat "tỷ" based on position (position 3 = "tỷ", position 6 = "tỷ tỷ", position 9 = "tỷ tỷ tỷ", etc.)
if (positionFromRight >= 6 && hasTrailingZeros) {
return ' tỷ tỷ'
const billionCount = Math.floor(positionFromRight / 3)
return ' ' + Array(billionCount).fill('tỷ').join(' ')
}
return ' tỷ'
}
Expand Down
18 changes: 18 additions & 0 deletions src/read/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,24 @@ describe('readVnNumber', () => {
expect(readVnNumber(BigInt('1000000000000000000'))).to.equal('một tỷ tỷ')
})

it('should handle octillions (tỷ tỷ tỷ)', () => {
expect(readVnNumber('1000000000000000000000000000')).to.equal(
'một tỷ tỷ tỷ',
)
expect(readVnNumber('12000000000000000000000000000')).to.equal(
'mười hai tỷ tỷ tỷ',
)
})

it('should handle undecillions (tỷ tỷ tỷ tỷ)', () => {
expect(readVnNumber('1000000000000000000000000000000000000')).to.equal(
'một tỷ tỷ tỷ tỷ',
)
expect(readVnNumber('12000000000000000000000000000000000000')).to.equal(
'mười hai tỷ tỷ tỷ tỷ',
)
})

it('should handle very large numbers with all groups', () => {
expect(readVnNumber('1234567890123456')).to.equal(
'một triệu hai trăm ba mươi bốn nghìn năm trăm sáu mươi bảy tỷ tám trăm chín mươi triệu một trăm hai mươi ba nghìn bốn trăm năm mươi sáu',
Expand Down
3 changes: 1 addition & 2 deletions src/read/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ import { splitIntoGroups } from './utils.ts'
* @return The Vietnamese number in string.
*/
export function readVnNumber(number: string | number | bigint): string {
const numStr = number.toString()
const groups = splitIntoGroups(numStr)
const groups = splitIntoGroups('' + number)
const groupTypes = calculateGroupTypes(groups.length)

const parts: string[] = []
Expand Down