-
Notifications
You must be signed in to change notification settings - Fork 0
/
numconword.go
73 lines (56 loc) · 2.73 KB
/
numconword.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package numconword
import (
"errors"
"github.com/shopspring/decimal"
"github.com/sujamess/numconword/convert"
"github.com/sujamess/numconword/options"
)
var (
// ErrInvalidNumber is an error when input an invalid number
ErrInvalidNumber = errors.New("numconword: invalid number")
)
// ConvertString executes a cv command and return readable words
// The opts parameter can be used to specify options for this converting (see the options.ConvertOptions documentation).
func ConvertString(num string, opts ...*options.ConvertOptions) (string, error) {
dec, err := decimal.NewFromString(num)
if err != nil {
return "", ErrInvalidNumber
}
return cv(dec, opts...)
}
// ConvertInt executes a cv command and return readable words
// The opts parameter can be used to specify options for this converting (see the options.ConvertOptions documentation).
func ConvertInt(num int, opts ...*options.ConvertOptions) (string, error) {
return cv(decimal.NewFromInt(int64(num)), opts...)
}
// ConvertInt32 executes a cv command and return readable words
// The opts parameter can be used to specify options for this converting (see the options.ConvertOptions documentation).
func ConvertInt32(num int32, opts ...*options.ConvertOptions) (string, error) {
return cv(decimal.NewFromInt32(num), opts...)
}
// ConvertInt64 executes a cv command and return readable words
// The opts parameter can be used to specify options for this converting (see the options.ConvertOptions documentation).
func ConvertInt64(num int64, opts ...*options.ConvertOptions) (string, error) {
return cv(decimal.NewFromInt(num), opts...)
}
// ConvertFloat32 executes a cv command and return readable words
// The opts parameter can be used to specify options for this converting (see the options.ConvertOptions documentation).
func ConvertFloat32(num float32, opts ...*options.ConvertOptions) (string, error) {
return cv(decimal.NewFromFloat32(num), opts...)
}
// ConvertFloat64 executes a cv command and return readable words
// The opts parameter can be used to specify options for this converting (see the options.ConvertOptions documentation).
func ConvertFloat64(num float64, opts ...*options.ConvertOptions) (string, error) {
return cv(decimal.NewFromFloat(num), opts...)
}
func cv(dec decimal.Decimal, opts ...*options.ConvertOptions) (string, error) {
i, d := splitNum(dec)
converter := &convert.Convert{Integer: i, Decimal: d, ConvertOptions: options.MergeConvertOptions(opts...)}
return converter.ToWords(), nil
}
func splitNum(n decimal.Decimal) (decimal.Decimal, decimal.Decimal) {
integer := decimal.NewFromInt(n.IntPart())
floating := n.Sub(integer)
floating = floating.Mul(decimal.NewFromInt(10).Pow(decimal.NewFromInt(int64(len(floating.String()) - 2))))
return integer, floating
}