Golang : Byte format example


Tags : golang round byte-format giga mega terra

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-how-to-declare-kilobyte-megabyte-gigabyte-terabyte-and-so-on

https://www.socketloop.com/tutorials/golang-round-float-to-precision-example

https://www.socketloop.com/tutorials/golang-convert-float-to-string



Tags : golang round byte-format giga mega terra

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