jnumber

package module
v0.6.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 2, 2023 License: MIT Imports: 10 Imported by: 0

README

JNumber - strconv for Japanese numerals

Go module that implements the conversion between UTF-8 encoded Japanese numerals and uint64/int64/big.Int.

Features

  • fast
  • zero/low allocations
  • zero external dependencies
  • supports conversion from/to int64, uint64 and big.Int
  • supports numbers |x| < 10^72 (as long as they fit into the used datatype)
  • supports daiji (大字), both current and obsolete ones
  • supports serial numbers like 二〇二三 for 2023
  • negative numbers use マイナス as a prefix

Examples

package main

import (
    "fmt"
    "math/big"
    
    "github.com/haesy/jnumber"
)

func main() {
    // int64/uint64/big.Int -> string
    fmt.Println(jnumber.FormatUint(299)) // "二百九十九"
    fmt.Println(jnumber.FormatInt(-299)) // "マイナス二百九十九"
    fmt.Println(jnumber.FormatBigInt(big.NewInt(299))) // "二百九十九"
    fmt.Println(jnumber.FormatSerialInt(2023)) // "二〇二三"

    // string -> int64/uint64/big.Int
    fmt.Println(jnumber.ParseUint("一千二百三十四")) // 1234
    fmt.Println(jnumber.ParseInt("マイナス二十三万四千五百六十七")) // -234567
    fmt.Println(jnumber.ParseInt("九百二十二京三千三百七十二兆三百六十八億五千四百七十七万五千八百七")) // 9223372036854775807
    fmt.Println(jnumber.ParseBigInt("一無量大数")) // 10^68
    fmt.Println(jnumber.ParseSerialInt("二〇二三")) // 2023
    
    // support for daiji
    fmt.Println(jnumber.ParseInt("弐千")) // 2000
    fmt.Println(jnumber.ParseInt("壱万")) // 10000

    // numeric value of a single kanji
    fmt.Println(jnumber.ValueOf('零')) // 0
    fmt.Println(jnumber.ValueOf('〇')) // 0
    fmt.Println(jnumber.ValueOf('一')) // 1
    fmt.Println(jnumber.ValueOf('二')) // 2
    fmt.Println(jnumber.ValueOf('三')) // 3
    fmt.Println(jnumber.ValueOf('十')) // 10
    fmt.Println(jnumber.ValueOf('万')) // 10000
}

Supported Numerals

Character Value Character Value
零 / 〇 0 1012
一 / 壱 * / 壹 * 1 京 ** 1016
二 / 弐 * / 貳 * 2 1020
三 / 参 * / 參 * 3 1024
四 / 肆 * 4 1028
五 / 伍 * 5 1032
六 / 陸 * 6 1036
七 / 柒 * / 漆 * 7 1040
八 / 捌 * 8 1044
九 / 玖 * 9 1048
十 / 拾 * 10 恒河沙 1052
百 / 佰 * 100 阿僧祇 1056
千 / 阡 * / 仟 * 1.000 那由他 1060
万 / 萬 * 104 不可思議 1064
108 無量大数 1068

* = Daiji / 大字

** = Biggest numeral that fits into int64/uint64

Contributing

If you find any bugs or want additional features please create an issue with details.

Merge requests out of the blue without any context or explanation are ignored.

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrEmpty is returned if a function does not allow empty strings as a parameter.
	ErrEmpty = errors.New("empty string")
	// ErrEOF is returned if a function expects a rune and detects an EOF instead.
	ErrEOF = errors.New("unexpected eof")
	// ErrOverflow is returned if the number is too big for the function.
	ErrOverflow = errors.New("number overflows datatype")
	// ErrEncoding ist returned if a function recognizes an invalid UTF-8 encoding.
	ErrEncoding = errors.New("invalid utf-8 encoding")
	// ErrInvalidSequence is returned if a string contains an invalid sequence of digits.
	// Examples: "一一" or "十百"
	ErrInvalidSequence = errors.New("invalid sequence of digits")
	// ErrUnexpectedRune is returned if a functions finds a rune that it does not expect.
	ErrUnexpectedRune = errors.New("unexpected rune")
)

Functions

func AppendInt added in v0.5.0

func AppendInt(dst []byte, i int64) []byte

func AppendSerialInt added in v0.6.0

func AppendSerialInt(dst []byte, i int64) []byte

func AppendSerialUint added in v0.6.0

func AppendSerialUint(dst []byte, u uint64) []byte

func AppendUint added in v0.5.0

func AppendUint(dst []byte, u uint64) []byte

func FormatBigInt

func FormatBigInt(i *big.Int) string

FormatBigInt returns the given big integer as a string of Japanese numerals. Supports only numbers |i| < 10^72.

func FormatInt

func FormatInt(i int64) string

FormatInt returns the given integer as a string of Japanese numerals.

func FormatSerialInt added in v0.6.0

func FormatSerialInt(i int64) string

FormatSerialInt returns the given integer as a string of Japanese numerals where the decimal digits 0 to 9 are replaced by the kanjis 〇 to 九.

func FormatSerialUint added in v0.6.0

func FormatSerialUint(u uint64) string

FormatSerialInt returns the given unsigned integer as a string of Japanese numerals where the decimal digits 0 to 9 are replaced by the kanjis 〇 to 九.

func FormatUint

func FormatUint(u uint64) string

FormatUint returns the given unsigned integer as a string of Japanese numerals.

func FromDaiji

func FromDaiji() *strings.Replacer

FromDaiji replaces daiji with regular kanji.

func ParseBigInt added in v0.2.0

func ParseBigInt(s string) (*big.Int, error)

ParseBigInt returns the integer represented by the given japanese numerals.

func ParseInt

func ParseInt(s string) (int64, error)

ParseInt returns the integer represented by the given japanese numerals.

func ParseSerialInt added in v0.6.0

func ParseSerialInt(s string) (int64, error)

ParseSerialInt returns the signed integer represented by the given japanese numerals.

func ParseSerialUint added in v0.6.0

func ParseSerialUint(s string) (uint64, error)

ParseSerialUint returns the unsigned integer represented by the given japanese numerals.

func ParseUint

func ParseUint(s string) (uint64, error)

ParseUint returns the unsigned integer represented by the given japanese numerals.

func ToDaiji

func ToDaiji() *strings.Replacer

ToDaiji replaces some kanji with current daiji (大字).

func ValueOf

func ValueOf(r rune) (value uint64, ok bool)

ValueOf returns the numeric value of a single kanji, if it has one.

Types

type SearchBigIntResult added in v0.4.0

type SearchBigIntResult struct {
	Start, End int
	Str        string
	Value      *big.Int
	Err        error
}

SearchBigIntResult is single match in a text that may be a japanese numeral.

func FindBigInt added in v0.4.0

func FindBigInt(s string) []*SearchBigIntResult

FindBigInt returns an array of all potential japanese numerals in the given string.

type SearchResult added in v0.4.0

type SearchResult struct {
	Start, End int
	Str        string
	Value      uint64
	Err        error
}

SearchResult is single match in a text that may be a japanese numeral.

func Find added in v0.4.0

func Find(s string) []*SearchResult

Find returns an array of all potential japanese numerals in the given string.

type UnexpectedRuneError

type UnexpectedRuneError struct {
	Actual, Expected rune
}

UnexpectedRuneError is returned if a functions finds a rune that it does not expect.

func (*UnexpectedRuneError) Error

func (e *UnexpectedRuneError) Error() string

func (*UnexpectedRuneError) Is added in v0.6.0

func (e *UnexpectedRuneError) Is(err error) bool

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL