Skip to content

Commit

Permalink
a2c-nums (#324)
Browse files Browse the repository at this point in the history
  • Loading branch information
soarowl20240613 authored Jan 8, 2024
1 parent 30e4f6f commit ba26ac2
Show file tree
Hide file tree
Showing 6 changed files with 307 additions and 0 deletions.
22 changes: 22 additions & 0 deletions packages/preview/a2c-nums/0.0.1/LICENSE
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.
39 changes: 39 additions & 0 deletions packages/preview/a2c-nums/0.0.1/README.md
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 added packages/preview/a2c-nums/0.0.1/demo.pdf
Binary file not shown.
127 changes: 127 additions & 0 deletions packages/preview/a2c-nums/0.0.1/demo.typ
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)
106 changes: 106 additions & 0 deletions packages/preview/a2c-nums/0.0.1/src/lib.typ
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
}
13 changes: 13 additions & 0 deletions packages/preview/a2c-nums/0.0.1/typst.toml
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"]

0 comments on commit ba26ac2

Please sign in to comment.