github.com/sandwich-go/boost@v1.3.29/xconv/num.go (about) 1 package xconv 2 3 import ( 4 "strconv" 5 ) 6 7 func ParseUint(s string) (uint, error) { 8 if v, err := strconv.ParseUint(s, 10, 0); err != nil { 9 return 0, err 10 } else { 11 return uint(v), nil 12 } 13 } 14 15 func ParseUint8(s string) (uint8, error) { 16 if v, err := strconv.ParseUint(s, 10, 8); err != nil { 17 return 0, err 18 } else { 19 return uint8(v), nil 20 } 21 } 22 23 func ParseUint16(s string) (uint16, error) { 24 if v, err := strconv.ParseUint(s, 10, 16); err != nil { 25 return 0, err 26 } else { 27 return uint16(v), nil 28 } 29 } 30 31 func ParseUint32(s string) (uint32, error) { 32 if v, err := strconv.ParseUint(s, 10, 32); err != nil { 33 return 0, err 34 } else { 35 return uint32(v), nil 36 } 37 } 38 39 func ParseUint64(s string) (uint64, error) { 40 return strconv.ParseUint(s, 10, 64) 41 } 42 43 func ParseInt(s string) (int, error) { 44 if v, err := strconv.ParseInt(s, 10, 0); err != nil { 45 return 0, err 46 } else { 47 return int(v), nil 48 } 49 } 50 51 func ParseInt8(s string) (int8, error) { 52 if v, err := strconv.ParseInt(s, 10, 8); err != nil { 53 return 0, err 54 } else { 55 return int8(v), nil 56 } 57 } 58 59 func ParseInt16(s string) (int16, error) { 60 if v, err := strconv.ParseInt(s, 10, 16); err != nil { 61 return 0, err 62 } else { 63 return int16(v), nil 64 } 65 } 66 67 func ParseInt32(s string) (int32, error) { 68 if v, err := strconv.ParseInt(s, 10, 32); err != nil { 69 return 0, err 70 } else { 71 return int32(v), nil 72 } 73 } 74 75 func ParseInt64(s string) (int64, error) { 76 return strconv.ParseInt(s, 10, 64) 77 } 78 79 func FormatInt(v int) string { 80 return strconv.FormatInt(int64(v), 10) 81 } 82 83 func FormatInt8(v int8) string { 84 return strconv.FormatInt(int64(v), 10) 85 } 86 87 func FormatInt16(v int16) string { 88 return strconv.FormatInt(int64(v), 10) 89 } 90 91 func FormatInt32(v int32) string { 92 return strconv.FormatInt(int64(v), 10) 93 } 94 95 func FormatInt64(v int64) string { 96 return strconv.FormatInt(int64(v), 10) 97 } 98 99 func FormatUint(v uint) string { 100 return strconv.FormatUint(uint64(v), 10) 101 } 102 103 func FormatUint8(v uint8) string { 104 return strconv.FormatUint(uint64(v), 10) 105 } 106 107 func FormatUint16(v uint16) string { 108 return strconv.FormatUint(uint64(v), 10) 109 } 110 111 func FormatUint32(v uint32) string { 112 return strconv.FormatUint(uint64(v), 10) 113 } 114 115 func FormatUint64(v uint64) string { 116 return strconv.FormatUint(uint64(v), 10) 117 } 118 119 func FormatIntToSlice(v int) []byte { 120 return strconv.AppendInt(nil, int64(v), 10) 121 } 122 123 func FormatInt8ToSlice(v int8) []byte { 124 return strconv.AppendInt(nil, int64(v), 10) 125 } 126 127 func FormatInt16ToSlice(v int16) []byte { 128 return strconv.AppendInt(nil, int64(v), 10) 129 } 130 131 func FormatInt32ToSlice(v int32) []byte { 132 return strconv.AppendInt(nil, int64(v), 10) 133 } 134 135 func FormatInt64ToSlice(v int64) []byte { 136 return strconv.AppendInt(nil, int64(v), 10) 137 } 138 139 func FormatUintToSlice(v uint) []byte { 140 return strconv.AppendUint(nil, uint64(v), 10) 141 } 142 143 func FormatUint8ToSlice(v uint8) []byte { 144 return strconv.AppendUint(nil, uint64(v), 10) 145 } 146 147 func FormatUint16ToSlice(v uint16) []byte { 148 return strconv.AppendUint(nil, uint64(v), 10) 149 } 150 151 func FormatUint32ToSlice(v uint32) []byte { 152 return strconv.AppendUint(nil, uint64(v), 10) 153 } 154 155 func FormatUint64ToSlice(v uint64) []byte { 156 return strconv.AppendUint(nil, uint64(v), 10) 157 }