go/num/str.go

158 lines
3.1 KiB
Go
Raw Permalink Normal View History

2014-09-11 09:23:43 +04:00
package num
import (
"strconv"
)
2014-09-24 06:21:44 +04:00
func ParseUint(s string) (uint, error) {
2014-09-11 09:23:43 +04:00
if v, err := strconv.ParseUint(s, 10, 0); err != nil {
return 0, err
} else {
return uint(v), nil
}
}
2014-09-24 06:21:44 +04:00
func ParseUint8(s string) (uint8, error) {
2014-09-11 09:23:43 +04:00
if v, err := strconv.ParseUint(s, 10, 8); err != nil {
return 0, err
} else {
return uint8(v), nil
}
}
2014-09-24 06:21:44 +04:00
func ParseUint16(s string) (uint16, error) {
2014-09-11 09:23:43 +04:00
if v, err := strconv.ParseUint(s, 10, 16); err != nil {
return 0, err
} else {
return uint16(v), nil
}
}
2014-09-24 06:21:44 +04:00
func ParseUint32(s string) (uint32, error) {
2014-09-11 09:23:43 +04:00
if v, err := strconv.ParseUint(s, 10, 32); err != nil {
return 0, err
} else {
return uint32(v), nil
}
}
2014-09-24 06:21:44 +04:00
func ParseUint64(s string) (uint64, error) {
2014-09-11 09:23:43 +04:00
return strconv.ParseUint(s, 10, 64)
}
2014-09-24 06:21:44 +04:00
func ParseInt(s string) (int, error) {
2014-09-11 09:23:43 +04:00
if v, err := strconv.ParseInt(s, 10, 0); err != nil {
return 0, err
} else {
return int(v), nil
}
}
2014-09-24 06:21:44 +04:00
func ParseInt8(s string) (int8, error) {
2014-09-11 09:23:43 +04:00
if v, err := strconv.ParseInt(s, 10, 8); err != nil {
return 0, err
} else {
return int8(v), nil
}
}
2014-09-24 06:21:44 +04:00
func ParseInt16(s string) (int16, error) {
2014-09-11 09:23:43 +04:00
if v, err := strconv.ParseInt(s, 10, 16); err != nil {
return 0, err
} else {
return int16(v), nil
}
}
2014-09-24 06:21:44 +04:00
func ParseInt32(s string) (int32, error) {
2014-09-11 09:23:43 +04:00
if v, err := strconv.ParseInt(s, 10, 32); err != nil {
return 0, err
} else {
return int32(v), nil
}
}
2014-09-24 06:21:44 +04:00
func ParseInt64(s string) (int64, error) {
2014-09-11 09:23:43 +04:00
return strconv.ParseInt(s, 10, 64)
}
2014-09-24 06:21:44 +04:00
func FormatInt(v int) string {
2014-09-11 09:23:43 +04:00
return strconv.FormatInt(int64(v), 10)
}
2014-09-24 06:21:44 +04:00
func FormatInt8(v int8) string {
2014-09-11 09:23:43 +04:00
return strconv.FormatInt(int64(v), 10)
}
2014-09-24 06:21:44 +04:00
func FormatInt16(v int16) string {
2014-09-11 09:23:43 +04:00
return strconv.FormatInt(int64(v), 10)
}
2014-09-24 06:21:44 +04:00
func FormatInt32(v int32) string {
2014-09-11 09:23:43 +04:00
return strconv.FormatInt(int64(v), 10)
}
2014-09-24 06:21:44 +04:00
func FormatInt64(v int64) string {
2014-09-11 09:23:43 +04:00
return strconv.FormatInt(int64(v), 10)
}
2014-09-24 06:21:44 +04:00
func FormatUint(v uint) string {
2014-09-11 09:23:43 +04:00
return strconv.FormatUint(uint64(v), 10)
}
2014-09-24 06:21:44 +04:00
func FormatUint8(v uint8) string {
2014-09-11 09:23:43 +04:00
return strconv.FormatUint(uint64(v), 10)
}
2014-09-24 06:21:44 +04:00
func FormatUint16(v uint16) string {
2014-09-11 09:23:43 +04:00
return strconv.FormatUint(uint64(v), 10)
}
2014-09-24 06:21:44 +04:00
func FormatUint32(v uint32) string {
2014-09-11 09:23:43 +04:00
return strconv.FormatUint(uint64(v), 10)
}
2014-09-24 06:21:44 +04:00
func FormatUint64(v uint64) string {
2014-09-11 09:23:43 +04:00
return strconv.FormatUint(uint64(v), 10)
}
2014-09-13 13:17:12 +04:00
2014-09-24 06:21:44 +04:00
func FormatIntToSlice(v int) []byte {
2014-09-13 13:17:12 +04:00
return strconv.AppendInt(nil, int64(v), 10)
}
2014-09-24 06:21:44 +04:00
func FormatInt8ToSlice(v int8) []byte {
2014-09-13 13:17:12 +04:00
return strconv.AppendInt(nil, int64(v), 10)
}
2014-09-24 06:21:44 +04:00
func FormatInt16ToSlice(v int16) []byte {
2014-09-13 13:17:12 +04:00
return strconv.AppendInt(nil, int64(v), 10)
}
2014-09-24 06:21:44 +04:00
func FormatInt32ToSlice(v int32) []byte {
2014-09-13 13:17:12 +04:00
return strconv.AppendInt(nil, int64(v), 10)
}
2014-09-24 06:21:44 +04:00
func FormatInt64ToSlice(v int64) []byte {
2014-09-13 13:17:12 +04:00
return strconv.AppendInt(nil, int64(v), 10)
}
2014-09-24 06:21:44 +04:00
func FormatUintToSlice(v uint) []byte {
2014-09-13 13:17:12 +04:00
return strconv.AppendUint(nil, uint64(v), 10)
}
2014-09-24 06:21:44 +04:00
func FormatUint8ToSlice(v uint8) []byte {
2014-09-13 13:17:12 +04:00
return strconv.AppendUint(nil, uint64(v), 10)
}
2014-09-24 06:21:44 +04:00
func FormatUint16ToSlice(v uint16) []byte {
2014-09-13 13:17:12 +04:00
return strconv.AppendUint(nil, uint64(v), 10)
}
2014-09-24 06:21:44 +04:00
func FormatUint32ToSlice(v uint32) []byte {
2014-09-13 13:17:12 +04:00
return strconv.AppendUint(nil, uint64(v), 10)
}
2014-09-24 06:21:44 +04:00
func FormatUint64ToSlice(v uint64) []byte {
2014-09-13 13:17:12 +04:00
return strconv.AppendUint(nil, uint64(v), 10)
}