mirror of
https://github.com/aykhans/go-utils.git
synced 2025-11-29 14:59:50 +00:00
feat: add the number package
This commit is contained in:
26
number/base.go
Normal file
26
number/base.go
Normal file
@@ -0,0 +1,26 @@
|
||||
package number
|
||||
|
||||
type Number interface {
|
||||
int | int8 | int16 | int32 | int64 |
|
||||
uint | uint8 | uint16 | uint32 | uint64 | uintptr
|
||||
}
|
||||
|
||||
func NumLen[T Number](number T) T {
|
||||
if number == 0 {
|
||||
return 1
|
||||
}
|
||||
|
||||
var count T = 0
|
||||
if number < 0 {
|
||||
for number < 0 {
|
||||
number /= 10
|
||||
count++
|
||||
}
|
||||
} else {
|
||||
for number > 0 {
|
||||
number /= 10
|
||||
count++
|
||||
}
|
||||
}
|
||||
return count
|
||||
}
|
||||
Reference in New Issue
Block a user