-
Notifications
You must be signed in to change notification settings - Fork 470
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
30e4f6f
commit ba26ac2
Showing
6 changed files
with
307 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
MIT License | ||
=========== | ||
|
||
Copyright (c) 2024 Zhuo Nengwen | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# a2c-nums | ||
|
||
Convert Arabic numbers to Chinese characters. | ||
|
||
## usage | ||
|
||
```typst | ||
#import "@preview/a2c-nums:0.0.1": int-to-cn-num, int-to-cn-ancient-num, int-to-cn-simple-num, num-to-cn-currency | ||
#int-to-cn-num(1234567890) | ||
#int-to-cn-ancient-num(1234567890) | ||
#int-to-cn-simple-num(2024) | ||
#num-to-cn-currency(1234567890.12) | ||
``` | ||
|
||
## Functions | ||
|
||
### int-to-cn-num | ||
|
||
Convert an integer to Chinese number. ex: `#int-to-cn-num(123)` will be `一百二十三` | ||
|
||
### int-to-cn-ancient-num | ||
|
||
Convert an integer to ancient Chinese number. ex: `#int-to-cn-ancient-num(123)` will be `壹佰贰拾叁` | ||
|
||
### int-to-cn-simple-num | ||
|
||
Convert an integer to simpple Chinese number. ex: `#int-to-cn-simple-num(2024)` will be `二〇二四` | ||
|
||
### num-to-cn-currency | ||
|
||
Convert a number to Chinese currency. ex: `#int-to-cn-simple-num(1234.56)` will be `壹仟贰佰叁拾肆元伍角陆分` | ||
|
||
### more details | ||
|
||
Reference [demo.typ](demo.typ) for more details please. |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
#import "@preview/a2c-nums:0.0.1": int-to-cn-num, int-to-cn-ancient-num, int-to-cn-simple-num, num-to-cn-currency | ||
|
||
#set heading(numbering: "1.1") | ||
|
||
= a2c-nums 示例 | ||
|
||
== int-to-cn-simple-num | ||
|
||
#int-to-cn-simple-num(2024) | ||
|
||
== int-to-cn-num 0-999 | ||
|
||
#{ | ||
let i = 0; | ||
while i < 1000 { | ||
int-to-cn-num(i) + " " | ||
i += 1 | ||
} | ||
} | ||
|
||
== others | ||
|
||
#int-to-cn-num(2024) | ||
|
||
#int-to-cn-num(987654321) | ||
|
||
=== 一个0: | ||
|
||
#int-to-cn-num(907654321) | ||
|
||
#int-to-cn-num(907054321) | ||
|
||
#int-to-cn-num(907050321) | ||
|
||
#int-to-cn-num(907050301) | ||
|
||
=== 两个0: | ||
|
||
#int-to-cn-num(900654321) | ||
|
||
#int-to-cn-num(980054321) | ||
|
||
#int-to-cn-num(987004321) | ||
|
||
#int-to-cn-num(987600321) | ||
|
||
#int-to-cn-num(987650021) | ||
|
||
#int-to-cn-num(987654001) | ||
|
||
#int-to-cn-num(987654300) | ||
|
||
=== 三个0: | ||
|
||
#int-to-cn-num(900054321) | ||
|
||
#int-to-cn-num(980004321) | ||
|
||
#int-to-cn-num(987000321) | ||
|
||
#int-to-cn-num(987600021) | ||
|
||
#int-to-cn-num(987650001) | ||
|
||
#int-to-cn-num(987654000) | ||
|
||
=== 四个0: | ||
|
||
#int-to-cn-num(900004321) | ||
|
||
#int-to-cn-num(980000321) | ||
|
||
#int-to-cn-num(987000021) | ||
|
||
#int-to-cn-num(987600001) | ||
|
||
#int-to-cn-num(987650000) | ||
|
||
=== 五个0: | ||
|
||
#int-to-cn-num(900000321) | ||
|
||
#int-to-cn-num(980000021) | ||
|
||
#int-to-cn-num(987000001) | ||
|
||
#int-to-cn-num(987600000) | ||
|
||
=== 六个0: | ||
|
||
#int-to-cn-num(900000021) | ||
|
||
#int-to-cn-num(980000001) | ||
|
||
#int-to-cn-num(987000000) | ||
|
||
=== 七个0: | ||
|
||
#int-to-cn-num(900000001) | ||
|
||
#int-to-cn-num(980000000) | ||
|
||
=== 八个0: | ||
|
||
#int-to-cn-num(900000000) | ||
|
||
== int-to-cn-ancient-num | ||
|
||
#int-to-cn-ancient-num(2024) | ||
|
||
#{ | ||
let i = 0; | ||
while i < 1000 { | ||
int-to-cn-ancient-num(i) + " " | ||
i += 1 | ||
} | ||
} | ||
|
||
== num-to-cn-currency | ||
|
||
#num-to-cn-currency(1234.56) | ||
|
||
#num-to-cn-currency(0.5678) | ||
|
||
#num-to-cn-currency(1203405602.5678) | ||
|
||
#num-to-cn-currency(234) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
// Convert an int to Chinese number, for ex: 2024 become "二〇二四" | ||
#let int-to-cn-simple-num(n) = { | ||
let digits = ("〇", "一", "二", "三", "四", "五", "六", "七", "八", "九") | ||
let s = str(n) | ||
let result = "" | ||
for c in s.codepoints() { | ||
result += digits.at(int(c)) | ||
} | ||
return result | ||
} | ||
|
||
// Convert an int string to Chinese number, for ex: "2024" become "二千零二十四" | ||
#let str-to-cn-num(s) = { | ||
let digits = ("零", "一", "二", "三", "四", "五", "六", "七", "八", "九") | ||
let units = ("", "十", "百", "千", "万", "十", "百", "千", "亿", "十", "百", "千") | ||
let result = "" | ||
let len = s.len() - 1 | ||
let i = len | ||
while i >= 0 { | ||
result = digits.at(int(s.at(i))) + units.at(len - i) + result; | ||
i -= 1 | ||
} | ||
|
||
for i in (0, 1, 2, 3) { | ||
result = result.replace("零亿", "亿") | ||
result = result.replace("零万", "万") | ||
result = result.replace("零千", "零") | ||
result = result.replace("零百", "零") | ||
result = result.replace("零十", "零") | ||
result = result.replace("零零", "零") | ||
result = result.replace("亿万", "亿") | ||
} | ||
if result.len() > 3 and result.ends-with("零") { | ||
result = result.trim("零") | ||
} | ||
if result.len() == 9 or result.len() == 6 { | ||
result = result.replace("一十", "十") | ||
} | ||
return result | ||
} | ||
|
||
// Convert an int to Chinese number, for ex: 2024 become "二千零二十四" | ||
#let int-to-cn-num(n) = { | ||
let s = str(n) | ||
return str-to-cn-num(s) | ||
} | ||
|
||
// Convert an int string to Chinese ancient number, for ex: "2024" become "贰仟零贰拾肆" | ||
#let str-to-cn-ancient-num(s) = { | ||
let digits = ("零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖") | ||
let units = ("", "拾", "佰", "仟", "万", "拾", "佰", "仟", "亿", "拾", "佰", "仟") | ||
let result = "" | ||
let len = s.len() - 1 | ||
let i = len | ||
while i >= 0 { | ||
result = digits.at(int(s.at(i))) + units.at(len - i) + result; | ||
i -= 1 | ||
} | ||
|
||
for i in (0, 1, 2, 3) { | ||
result = result.replace("零亿", "亿") | ||
result = result.replace("零万", "万") | ||
result = result.replace("零仟", "零") | ||
result = result.replace("零佰", "零") | ||
result = result.replace("零拾", "零") | ||
result = result.replace("零零", "零") | ||
result = result.replace("亿万", "亿") | ||
} | ||
if result.len() > 3 and result.ends-with("零") { | ||
result = result.trim("零") | ||
} | ||
if result.len() == 9 or result.len() == 6 { | ||
result = result.replace("壹拾", "拾") | ||
} | ||
return result | ||
} | ||
|
||
// Convert an int to Chinese ancient number, for ex: 2024 become "贰仟零贰拾肆" | ||
#let int-to-cn-ancient-num(n) = { | ||
let s = str(n) | ||
return str-to-cn-ancient-num(s) | ||
} | ||
|
||
// Convert a number to Chinese currency, for ex: 1234.56 become "壹仟贰佰叁拾肆元伍角陆分" | ||
#let num-to-cn-currency(n) = { | ||
let digits = ("零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖") | ||
let units = ("角", "分") | ||
let intpart = "" | ||
let decimal = "" | ||
let value = str(calc.round(n, digits: 2)) | ||
let splits = value.split(".") | ||
intpart = splits.at(0) | ||
if splits.len() > 1 { | ||
decimal = splits.at(1) | ||
} | ||
let result = "" | ||
if decimal != none { | ||
for (i, c) in decimal.codepoints().enumerate() { | ||
if i <= 1 { | ||
result += digits.at(int(c)) + units.at(i) | ||
} | ||
} | ||
} | ||
result = str-to-cn-ancient-num(intpart) + "元" + result | ||
return result | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
[package] | ||
name = "a2c-nums" | ||
version = "0.0.1" | ||
authors = ["Zhuo Nengwen <soarowl@yeah.net>"] | ||
license = "MIT" | ||
description = "Convert a number to Chinese" | ||
repository = "https://github.com/soarowl/a2c-nums.git" | ||
|
||
keywords = ["Converter", "number", "Chinese", "Currency"] | ||
|
||
entrypoint = "src/lib.typ" | ||
compiler = "0.10.0" | ||
exclude = ["demo.pdf"] |