1
0
mirror of https://github.com/fumiama/go-docx.git synced 2026-06-28 07:00:23 +08:00

fix #9: float point parse value

This commit is contained in:
源文雨
2023-07-10 14:36:09 +08:00
parent 1a682f16d9
commit 9f3162a90f
10 changed files with 162 additions and 199 deletions

View File

@@ -21,6 +21,8 @@
package docx
import (
"fmt"
"strconv"
"unsafe"
)
@@ -38,3 +40,31 @@ func StringToBytes(s string) (b []byte) {
bh.cap = sh.len
return b
}
// GetInt64 from string
func GetInt64(s string) (int64, error) {
v, err := strconv.ParseInt(s, 10, 64)
if err == nil {
return v, nil
}
v2, err := strconv.ParseFloat(s, 64)
if err == nil {
return int64(v2), nil
}
_, err = fmt.Sscanf(s, "%d", &v)
return v, err
}
// GetInt from string
func GetInt(s string) (int, error) {
v, err := strconv.Atoi(s)
if err == nil {
return v, nil
}
v2, err := strconv.ParseFloat(s, 64)
if err == nil {
return int(v2), nil
}
_, err = fmt.Sscanf(s, "%d", &v)
return v, err
}