new file: gn/convert.go

new file:   gn/convert_test.go
This commit is contained in:
Teodros 2024-06-01 08:50:23 +10:00
parent 593b4ac3ba
commit 96f395dc52
2 changed files with 297 additions and 0 deletions

169
gn/convert.go Normal file
View File

@ -0,0 +1,169 @@
package gn
import (
"fmt"
"math"
"strconv"
"strings"
)
// Prsfloat return the float64 value parsing geez string g.
func Prsfloat(g string) float64 {
if !strings.Contains(g, ".") {
//i, _ := strconv.Atoi(str)
i := Prsint(g)
f := float64(i)
return f
}
strs := strings.Split(g, ".")
s0 := Prsint(strs[0])
s1 := Prsint(strs[1])
res := fmt.Sprint(s0) + "." + fmt.Sprint(s1)
f, _ := strconv.ParseFloat(res, 64)
return f
}
// Prsint return the intiger value parsing geez string g.
func Prsint(g string) int {
if strings.Contains(g, ".") {
return 0
}
var signed bool
if len(g) > 1 && g[0] == '-' {
g = g[1:]
signed = true
}
lst := strings.SplitAfter(g, "፻")
var zz []int
for idx, x := range lst {
var z1, z2 int
if strings.Contains(x, "፻") {
if x == "፻" {
y := prsrune('፩')
z1 = y * int(math.Pow10(2*((len(lst)-1)-idx)))
} else {
l := strings.Split(x, "፻")
for _, v := range l[0] {
y := prsrune(v)
y = y * int(math.Pow10(2*((len(lst)-1)-idx)))
z1 += y
}
}
zz = append(zz, z1)
} else {
for _, v := range x {
y := prsrune(v)
y = y * int(math.Pow10(2*((len(lst)-1)-idx)))
z2 += y
}
zz = append(zz, z2)
}
}
var i int
for _, v := range zz {
i += v
}
if signed {
i = -1 * i
}
return i
}
func prsrune(g rune) int {
dif := '፩' - '1'
if g == '፻' {
return 100
}
if '፱' < g && g < '፻' {
r := g - dif - 9
i, _ := strconv.Atoi(string(r) + "0")
return i
}
if '፨' <= g && g <= '፱' {
r := g - dif
i, _ := strconv.Atoi(string(r))
return i
}
return 0
}
// Fmtint return string representation of integer i
func Fmtint(i int) string {
var signed bool
if i < 0 {
i = -1 * i
signed = true
}
str := strconv.Itoa(i)
var zz []rune
for idx, v := range str {
b := (len(str) - 1) - idx
q := b % 2
if q != 0 {
i, _ := strconv.Atoi(string(v))
x := fmtrune(i * 10)
zz = append(zz, x)
} else {
i, _ := strconv.Atoi(string(v))
x := fmtrune(i)
if idx != len(str)-1 {
zz = append(zz, x, '፻')
} else {
zz = append(zz, x)
}
}
}
var ans []rune
for _, v := range zz {
if v != '፨' {
ans = append(ans, v)
}
}
if len(ans) >= 2 && ans[0] == '፩' && ans[1] == '፻' {
ans = ans[1:]
}
var answer string
switch signed {
case true:
answer = "-" + string(ans)
case false:
answer = string(ans)
}
return answer
}
func fmtrune(i int) rune {
dif := '፩' - '1'
if i == 100 {
return '፻'
}
if 10 <= i && i <= 90 {
r := dif + '1' + rune(i)/10 + 8
return r
}
if 0 < i && i <= 9 {
r := dif + '1' + rune(i) - 1
return r
}
return '፨'
}
// Fmtfloat return string representation of float64 n
func Fmtfloat(n float64) string {
str := strconv.FormatFloat(n, 'f', -1, 64)
if !strings.Contains(str, ".") {
i, _ := strconv.Atoi(str)
s := Fmtint(i)
return s
}
strs := strings.Split(str, ".")
i, _ := strconv.Atoi(strs[0])
s0 := Fmtint(i)
q := len(strs[1]) % 2
j, _ := strconv.Atoi(strs[1])
if q != 0 {
j, _ = strconv.Atoi(strs[1] + "0")
}
s1 := Fmtint(j)
return s0 + "." + s1
}

128
gn/convert_test.go Normal file
View File

@ -0,0 +1,128 @@
package gn
import (
"fmt"
"testing"
)
func TestPrsint(t *testing.T) {
tests := []struct {
in string
want int
}{
{"", 0},
{"+", 0},
{"-", 0},
{"-፲፱፻፴፩", -1931},
{"+፴", 30},
{"፲፱", 19},
{"፱", 9},
{"፻", 100},
{"፲፻፱፻", 100900},
}
//fmt.Println("---Prsint testing---")
for _, v := range tests {
got := Prsint(v.in)
if got != v.want {
t.Errorf("want %v got %v", v.want, got)
}
//fmt.Printf("want %v got %v\n", v.want, got)
}
}
func TestFmtint(t *testing.T) {
tests := []struct {
in int
want string
}{
//{10000, "፼"},
{100, "፻"},
{90, "፺"},
{10, "፲"},
{9, "፱"},
{1, "፩"},
//{0, "፨"},
{0, ""},
{+19, "፲፱"},
{-1931, "-፲፱፻፴፩"},
{100900, "፲፻፱፻"},
//{10000, "ሐ"},
}
//fmt.Println("---Fmtint testing---")
for _, v := range tests {
got := Fmtint(v.in)
if got != v.want {
t.Errorf("want %s got %s", v.want, got)
}
//fmt.Printf("want %s got %s\n", v.want, got)
}
}
func TestFmtfloat(t *testing.T) {
tests := []struct {
in float64
want string
}{
//{10000, "፼"},
{100.11, "፻.፲፩"},
{90.2, "፺.፳"},
{10.345, "፲.፴፬፻፶"},
{9.41, "፱.፵፩"},
{1.0, "፩"},
//{0, "፨"},
{0.0, ""},
{+19.0, "፲፱"},
{-1931.0, "-፲፱፻፴፩"},
{100900.0, "፲፻፱፻"},
//{10000, "ሐ"},
}
//fmt.Println("---Fmtfloat testing---")
for _, v := range tests {
got := Fmtfloat(v.in)
if got != v.want {
t.Errorf("want %s got %s", v.want, got)
}
//fmt.Printf("want %s got %s\n", v.want, got)
}
}
func TestPrsfloat(t *testing.T) {
tests := []struct {
in string
want float64
}{
{"", 0},
{"+", 0},
{"-", 0},
{"-፲፱፻፴፩", -1931},
{"+፴", 30},
{"፲፱.፲", 19.1},
{"፱.", 9},
{"፻", 100},
{"፻.፲፩", 100.11},
{"፲፻፱፻", 100900},
}
//fmt.Println("---Prsfloat testing---")
for _, v := range tests {
got := Prsfloat(v.in)
if got != v.want {
t.Errorf("want %v got %v", v.want, got)
}
//fmt.Printf("want %v got %v\n", v.want, got)
}
}
func ExamplePrsint() {
i := Prsint("፲፱፻፴፩")
fmt.Println(i)
//output: 1931
}
func ExampleFmtfloat() {
i := Fmtfloat(9.41)
fmt.Println(i)
//output: ፱.፵፩
}
func ExamplePrsfloat() {
i := Prsfloat("-፱፻፴.፵፩")
fmt.Println(i)
//output: -930.41
}