github.com/searKing/golang/go@v1.2.117/format/multiple_prefix/binary_format.go (about) 1 // Copyright 2020 The searKing Author. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package multiple_prefix 6 7 import ( 8 "strings" 9 "unicode" 10 11 strings_ "github.com/searKing/golang/go/strings" 12 ) 13 14 func BinaryFormatInt(number int, precision int) string { 15 return BinaryFormatInt64(int64(number), precision) 16 } 17 18 func BinaryFormatUint(number uint, precision int) string { 19 return BinaryFormatUint64(uint64(number), precision) 20 } 21 22 func BinaryFormatInt64(number int64, precision int) string { 23 return BinaryMultiplePrefixTODO.Copy().SetInt64(number).FormatInt64(number, precision) 24 } 25 26 func BinaryFormatUint64(number uint64, precision int) string { 27 return BinaryMultiplePrefixTODO.Copy().SetUint64(number).FormatUint64(number, precision) 28 } 29 30 func BinaryFormatFloat(number float64, precision int) string { 31 return BinaryMultiplePrefixTODO.Copy().SetFloat64(number).FormatFloat(number, precision) 32 } 33 34 // SplitBinary splits s into number, multiple_prefix and unparsed strings 35 func SplitBinary(s string) (number string, prefix *BinaryMultiplePrefix, unparsed string) { 36 splits := strings_.SplitPrefixNumber(s) 37 if len(splits) < 2 { 38 return "", nil, unparsed 39 } 40 number = splits[0] 41 unparsed = splits[1] 42 // trim any space between numbers and symbols 43 unparsed = strings.TrimLeftFunc(splits[1], unicode.IsSpace) 44 45 for _, prefix := range binaryPositiveMultiplePrefixes { 46 if strings.HasPrefix(unparsed, prefix.Symbol()) { 47 return number, prefix.Copy(), strings.TrimPrefix(unparsed, prefix.Symbol()) 48 } 49 } 50 for _, prefix := range binaryNegativeMultiplePrefixes { 51 if strings.HasPrefix(unparsed, prefix.Symbol()) { 52 return number, prefix.Copy(), strings.TrimPrefix(unparsed, prefix.Symbol()) 53 } 54 } 55 for _, prefix := range binaryZeroMultiplePrefixes { 56 if strings.HasPrefix(unparsed, prefix.Symbol()) { 57 return number, prefix.Copy(), strings.TrimPrefix(unparsed, prefix.Symbol()) 58 } 59 } 60 return number, nil, unparsed 61 }