|
1 | | -# vn-number 🇻🇳 |
| 1 | +# vn-number |
2 | 2 |
|
3 | | -🛠 A bunch of utility functions that work with number in 🇻🇳 Vietnamese language |
| 3 | +A bunch of utility functions that work with numbers in Vietnamese language. |
4 | 4 |
|
5 | 5 | [](https://github.com/hckhanh/vn-number/actions/workflows/publish.yml) |
6 | 6 | [](https://sonarcloud.io/summary/new_code?id=hckhanh_vn-number) |
|
14 | 14 | - Built-in support for Edge runtime |
15 | 15 | - Typesafe with TypeScript |
16 | 16 | - Fully documented |
| 17 | +- Works with `number`, `string`, and `bigint` types |
| 18 | +- Handles very large numbers (up to quintillions) |
17 | 19 |
|
18 | | -## Functions |
| 20 | +## Installation |
19 | 21 |
|
20 | | -### Read Vietnamese number |
| 22 | +```bash |
| 23 | +npm install vn-number |
| 24 | +``` |
| 25 | + |
| 26 | +```bash |
| 27 | +deno add jsr:@hckhanh/vn-number |
| 28 | +``` |
| 29 | + |
| 30 | +## Quick Start |
| 31 | + |
| 32 | +### Read Vietnamese Numbers |
| 33 | + |
| 34 | +Convert numbers to Vietnamese text: |
| 35 | + |
| 36 | +```ts |
| 37 | +import { readVnNumber } from 'vn-number' |
| 38 | + |
| 39 | +readVnNumber(1250000) |
| 40 | +// Output: "một triệu hai trăm năm mươi nghìn" |
| 41 | + |
| 42 | +readVnNumber(15) |
| 43 | +// Output: "mười lăm" |
| 44 | + |
| 45 | +readVnNumber(21) |
| 46 | +// Output: "hai mươi mốt" |
| 47 | +``` |
| 48 | + |
| 49 | +### Format Numbers in Vietnamese Style |
| 50 | + |
| 51 | +Format numbers with Vietnamese thousand separators (dots): |
| 52 | + |
| 53 | +```ts |
| 54 | +import { formatVnNumber } from 'vn-number' |
| 55 | + |
| 56 | +formatVnNumber(1250000) |
| 57 | +// Output: "1.250.000" |
| 58 | + |
| 59 | +formatVnNumber(BigInt('9999999999999999')) |
| 60 | +// Output: "9.999.999.999.999.999" |
| 61 | +``` |
| 62 | + |
| 63 | +### Format Vietnamese Currency (VND) |
| 64 | + |
| 65 | +Format numbers as Vietnamese Dong currency: |
| 66 | + |
| 67 | +```ts |
| 68 | +import { formatVnCurrency } from 'vn-number' |
| 69 | + |
| 70 | +formatVnCurrency(1250000) |
| 71 | +// Output: "1.250.000 ₫" |
| 72 | + |
| 73 | +formatVnCurrency(null, 'Không giới hạn') |
| 74 | +// Output: "Không giới hạn" |
| 75 | +``` |
| 76 | + |
| 77 | +### Format Percentages |
| 78 | + |
| 79 | +Format numbers as Vietnamese-style percentages: |
21 | 80 |
|
22 | 81 | ```ts |
23 | | -import { readVnNumber } from '@hckhanh/vn-number' |
| 82 | +import { formatVnPercent } from 'vn-number' |
| 83 | + |
| 84 | +formatVnPercent(0.991) |
| 85 | +// Output: "99,1%" |
24 | 86 |
|
25 | | -const result = readVnNumber(1250000) |
26 | | -console.log(result) // một triệu hai trăm năm mươi nghìn |
| 87 | +formatVnPercent(0.5) |
| 88 | +// Output: "50%" |
27 | 89 | ``` |
28 | 90 |
|
29 | | -### Format number in Vietnamese format |
| 91 | +## Documentation |
| 92 | + |
| 93 | +For detailed documentation, examples, and API reference, visit: |
| 94 | + |
| 95 | +**[https://vn-number.khanh.id](https://vn-number.khanh.id)** |
| 96 | + |
| 97 | +## Common Use Cases |
| 98 | + |
| 99 | +### E-commerce |
30 | 100 |
|
31 | 101 | ```ts |
32 | | -import { formatVnNumber } from '@hckhanh/vn-number' |
| 102 | +import { formatVnCurrency, readVnNumber } from 'vn-number' |
| 103 | + |
| 104 | +const price = 1500000 |
| 105 | + |
| 106 | +console.log(formatVnCurrency(price)) |
| 107 | +// Output: "1.500.000 ₫" |
33 | 108 |
|
34 | | -const result = formatVnNumber(1250000) |
35 | | -console.log(result) // 1.250.000 |
| 109 | +console.log(readVnNumber(price)) |
| 110 | +// Output: "một triệu năm trăm nghìn" |
36 | 111 | ``` |
37 | 112 |
|
38 | | -### Format VN currency (VND - ₫) |
| 113 | +### Banking and Financial Documents |
39 | 114 |
|
40 | 115 | ```ts |
41 | | -import { formatVnCurrency } from '@hckhanh/vn-number' |
| 116 | +import { readVnNumber, formatVnCurrency } from 'vn-number' |
42 | 117 |
|
43 | | -const result = formatVnCurrency(1250000) |
44 | | -console.log(result) // 1.250.000 ₫ |
| 118 | +const amount = 2450000 |
| 119 | + |
| 120 | +console.log(`Số tiền: ${formatVnCurrency(amount)}`) |
| 121 | +// Output: "Số tiền: 2.450.000 ₫" |
| 122 | + |
| 123 | +console.log(`Bằng chữ: ${readVnNumber(amount)} đồng`) |
| 124 | +// Output: "Bằng chữ: hai triệu bốn trăm năm mươi nghìn đồng" |
45 | 125 | ``` |
46 | 126 |
|
47 | | -### Format percentage in Vietnamese format |
| 127 | +### Data Visualization |
48 | 128 |
|
49 | 129 | ```ts |
50 | | -import { formatVnPercent } from '@hckhanh/vn-number' |
| 130 | +import { formatVnNumber, formatVnPercent } from 'vn-number' |
| 131 | + |
| 132 | +const totalUsers = 1234567 |
| 133 | +const growthRate = 0.157 |
51 | 134 |
|
52 | | -const result = formatVnPercent(0.991) |
53 | | -console.log(result) // 99,1% |
| 135 | +console.log(`Tổng người dùng: ${formatVnNumber(totalUsers)}`) |
| 136 | +// Output: "Tổng người dùng: 1.234.567" |
| 137 | + |
| 138 | +console.log(`Tăng trưởng: ${formatVnPercent(growthRate)}`) |
| 139 | +// Output: "Tăng trưởng: 15,7%" |
54 | 140 | ``` |
55 | 141 |
|
| 142 | +## Vietnamese Language Rules |
| 143 | + |
| 144 | +The `readVnNumber` function follows Vietnamese language conventions: |
| 145 | + |
| 146 | +- **"lăm" rule**: 15 → "mười lăm", 25 → "hai mươi lăm" |
| 147 | +- **"mốt" rule**: 21 → "hai mươi mốt", 31 → "ba mươi mốt" |
| 148 | +- **"lẻ" rule**: 101 → "một trăm lẻ một", 305 → "ba trăm lẻ năm" |
| 149 | +- **Zero handling**: 1001 → "một nghìn không trăm lẻ một" |
| 150 | + |
| 151 | +## API Functions |
| 152 | + |
| 153 | +| Function | Description | Example | |
| 154 | +|----------|-------------|---------| |
| 155 | +| `readVnNumber(number)` | Convert number to Vietnamese text | `readVnNumber(1250000)` → `"một triệu hai trăm năm mươi nghìn"` | |
| 156 | +| `formatVnNumber(number, fallback?)` | Format number in Vietnamese style | `formatVnNumber(1250000)` → `"1.250.000"` | |
| 157 | +| `formatVnCurrency(money, fallback?)` | Format as VND currency | `formatVnCurrency(1250000)` → `"1.250.000 ₫"` | |
| 158 | +| `formatVnPercent(value, fallback?)` | Format as percentage | `formatVnPercent(0.991)` → `"99,1%"` | |
| 159 | + |
| 160 | +## Browser and Runtime Compatibility |
| 161 | + |
| 162 | +- ✅ Node.js 14+ |
| 163 | +- ✅ Bun |
| 164 | +- ✅ Deno |
| 165 | +- ✅ Modern browsers (Chrome, Firefox, Safari, Edge) |
| 166 | +- ✅ React Native |
| 167 | +- ✅ Electron |
| 168 | +- ✅ Edge Runtime (Vercel, Cloudflare Workers, etc.) |
| 169 | + |
| 170 | +## Contributing |
| 171 | + |
| 172 | +Contributions are welcome! Please feel free to submit a Pull Request. |
| 173 | + |
| 174 | +## License |
| 175 | + |
| 176 | +MIT © [Khánh Hoàng](https://www.khanh.id) |
| 177 | + |
56 | 178 | ## Release Notes |
57 | 179 |
|
58 | | -You can go to [the Releases](https://github.com/hckhanh/vn-number/releases) page to see the release notes. |
| 180 | +See [Releases](https://github.com/hckhanh/vn-number/releases) for changelog and release notes. |
0 commit comments