Skip to content

Bytes returns 32 MB for 31450000 bytes instead of 31 MB #103

Open
@ravibitsgoa

Description

@ravibitsgoa

Sample program

package main

import (
	"fmt"
	"github.com/dustin/go-humanize"
)

func main() {
	fmt.Println(humanize.Bytes(31350000))
	fmt.Println(humanize.Bytes(31450000))
}

Current output

The above program, compiled with the latest main branch prints 31 MB and 32 MB.

Expected output

The result should be 31 MB for both calls. This is because 31.45 is less than 31.5. Hence, should be rounded down.
This could be happening because of rounding errors in floating points.

Also, as a side note, the default rounding behaviour of the Bytes function is not documented properly.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions