Golang : Byte format example
Problem:
You want to format the long integer or number string representing a byte size such as giga, mega, peta or exa bytes into a shorter form to readability purpose. Such as formating 1234567891
to 1.1 GB
.
Solution:
This ByteFormat()
function will convert a given size in float64
type into a formatted string plus the measurement unit.
Here you go!
package main
import (
"fmt"
"math"
"strconv"
)
func RoundUp(input float64, places int) (newVal float64) {
var round float64
pow := math.Pow(10, float64(places))
digit := pow * input
round = math.Ceil(digit)
newVal = round / pow
return
}
func ByteFormat(inputNum float64, precision int) string {
if precision <= 0 {
precision = 1
}
var unit string
var returnVal float64
if inputNum >= 1000000000000000000000000 {
returnVal = RoundUp((inputNum / 1208925819614629174706176), precision)
unit = " YB" // yottabyte
} else if inputNum >= 1000000000000000000000 {
returnVal = RoundUp((inputNum / 1180591620717411303424), precision)
unit = " ZB" // zettabyte
} else if inputNum >= 10000000000000000000 {
returnVal = RoundUp((inputNum / 1152921504606846976), precision)
unit = " EB" // exabyte
} else if inputNum >= 1000000000000000 {
returnVal = RoundUp((inputNum / 1125899906842624), precision)
unit = " PB" // petabyte
} else if inputNum >= 1000000000000 {
returnVal = RoundUp((inputNum / 1099511627776), precision)
unit = " TB" // terrabyte
} else if inputNum >= 1000000000 {
returnVal = RoundUp((inputNum / 1073741824), precision)
unit = " GB" // gigabyte
} else if inputNum >= 1000000 {
returnVal = RoundUp((inputNum / 1048576), precision)
unit = " MB" // megabyte
} else if inputNum >= 1000 {
returnVal = RoundUp((inputNum / 1024), precision)
unit = " KB" // kilobyte
} else {
returnVal = inputNum
unit = " bytes" // byte
}
return strconv.FormatFloat(returnVal, 'f', precision, 64) + unit
}
func main() {
fmt.Println("-----------------------------------------------------------------")
fmt.Println("Byte formatting with PRECISION = 1 example")
fmt.Println("-----------------------------------------------------------------")
fmt.Println("123 = ", ByteFormat(123, 1))
fmt.Println("1234 = ", ByteFormat(1234, 1))
fmt.Println("1234567 = ", ByteFormat(1234567, 1))
fmt.Println("1234567891 = ", ByteFormat(1234567891, 1))
fmt.Println("12345678912345 = ", ByteFormat(12345678912345, 1))
fmt.Println("123456789123456789 = ", ByteFormat(123456789123456789, 1))
fmt.Println("1234567891234567891234 = ", ByteFormat(1234567891234567891234, 1))
fmt.Println("123456789123456789123456789 = ", ByteFormat(123456789123456789123456789, 1))
fmt.Println("-----------------------------------------------------------------")
fmt.Println("Byte formatting with PRECISION = 4 example")
fmt.Println("-----------------------------------------------------------------")
fmt.Println("123 = ", ByteFormat(123, 4))
fmt.Println("1234 = ", ByteFormat(1234, 4))
fmt.Println("1234567 = ", ByteFormat(1234567, 4))
fmt.Println("1234567891 = ", ByteFormat(1234567891, 4))
fmt.Println("12345678912345 = ", ByteFormat(12345678912345, 4))
fmt.Println("123456789123456789 = ", ByteFormat(123456789123456789, 4))
fmt.Println("1234567891234567891234 = ", ByteFormat(1234567891234567891234, 4))
fmt.Println("123456789123456789123456789 = ", ByteFormat(123456789123456789123456789, 4))
}
Output:
-----------------------------------------------------------------
Byte formatting with PRECISION = 1 example
-----------------------------------------------------------------
123 = 123.0 bytes
1234 = 1.2 KB
1234567 = 1.2 MB
1234567891 = 1.1 GB
12345678912345 = 11.2 TB
123456789123456789 = 109.7 PB
1234567891234567891234 = 1.0 ZB
123456789123456789123456789 = 102.1 YB
-----------------------------------------------------------------
Byte formatting with PRECISION = 4 example
-----------------------------------------------------------------
123 = 123.0000 bytes
1234 = 1.2051 KB
1234567 = 1.1774 MB
1234567891 = 1.1498 GB
12345678912345 = 11.2284 TB
123456789123456789 = 109.6517 PB
1234567891234567891234 = 1.0458 ZB
123456789123456789123456789 = 102.1211 YB
Happy coding!
References:
https://www.socketloop.com/tutorials/golang-round-float-to-precision-example
https://www.socketloop.com/tutorials/golang-convert-float-to-string
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+8k Golang : Mapping Iban to Dunging alphabets
+13k Golang : flag provided but not defined error
+16.5k Golang : convert string or integer to big.Int type
+55.6k Golang : Unmarshal JSON from http response
+4.8k Linux : sudo yum updates not working
+6.5k Golang : Detect face in uploaded photo like GPlus
+36.5k Golang : Convert(cast) int64 to string
+10.2k Golang : Channels and buffered channels examples
+8.9k Python : Fix SyntaxError: Non-ASCII character in file, but no encoding declared
+16.2k Golang : How to reverse elements order in map ?
+4.9k Linux/MacOSX : How to symlink a file?
+10k Golang : Resumable upload to Google Drive(RESTful) example