jnumber

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Aug 11, 2020 License: MIT Imports: 7 Imported by: 0

README

JNumber

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

Support for big.Int is limited to FormatBigInt, ParseBigInt is not implemented yet.

Negative numbers use the char '-' (0x45) as prefix and are mainly implemented to allow serialization/deserialization without losing information.

Examples

package main

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

func main() {
    // int64/uint64 -> string
    fmt.Println(FormatUint(299)) // "二百九十九"
    fmt.Println(FormatInt(-299)) // "-二百九十九"

    // string -> int64/uint64
    fmt.Println(ParseUint("一千二百三十四")) // 1234
    fmt.Println(ParseInt("-二十三万四千五百六十七")) // -234567
    fmt.Println(ParseInt("九百二十二京三千三百七十二兆三百六十八億五千四百七十七万五千八百七") // 9223372036854775807
    
    // support for daiji
    fmt.Println(ParseInt("弐千")) // 2000
    fmt.Println(ParseInt("壱万")) // 10000

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

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 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 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 current 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 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 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) Unwrap

func (e *UnexpectedRuneError) Unwrap() error

Jump to

Keyboard shortcuts

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