Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1093823
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T23:52:57+00:00 2026-05-16T23:52:57+00:00

How can I convert byte size into a human-readable format in Java? Like 1024

  • 0

How can I convert byte size into a human-readable format in Java?

Like 1024 should become "1 Kb" and 1024*1024 should become "1 Mb".

I am kind of sick of writing this utility method for each project. Is there a static method in Apache Commons for this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T23:52:58+00:00Added an answer on May 16, 2026 at 11:52 pm

    Fun fact: The original snippet posted here was the most copied Java snippet of all time on Stack Overflow, and it was flawed. It was fixed, but it got messy.

    Full story in this article: The most copied Stack Overflow snippet of all time is flawed!

    Source: Formatting byte size to human readable format | Programming.Guide

    SI (1 k = 1,000)

    public static String humanReadableByteCountSI(long bytes) {
        if (-1000 < bytes && bytes < 1000) {
            return bytes + " B";
        }
        CharacterIterator ci = new StringCharacterIterator("kMGTPE");
        while (bytes <= -999_950 || bytes >= 999_950) {
            bytes /= 1000;
            ci.next();
        }
        return String.format("%.1f %cB", bytes / 1000.0, ci.current());
    }
    

    Binary (1 Ki = 1,024)

    public static String humanReadableByteCountBin(long bytes) {
        long absB = bytes == Long.MIN_VALUE ? Long.MAX_VALUE : Math.abs(bytes);
        if (absB < 1024) {
            return bytes + " B";
        }
        long value = absB;
        CharacterIterator ci = new StringCharacterIterator("KMGTPE");
        for (int i = 40; i >= 0 && absB > 0xfffccccccccccccL >> i; i -= 10) {
            value >>= 10;
            ci.next();
        }
        value *= Long.signum(bytes);
        return String.format("%.1f %ciB", value / 1024.0, ci.current());
    }
    

    Example output:

                                 SI     BINARY
    
                      0:        0 B        0 B
                     27:       27 B       27 B
                    999:      999 B      999 B
                   1000:     1.0 kB     1000 B
                   1023:     1.0 kB     1023 B
                   1024:     1.0 kB    1.0 KiB
                   1728:     1.7 kB    1.7 KiB
                 110592:   110.6 kB  108.0 KiB
                7077888:     7.1 MB    6.8 MiB
              452984832:   453.0 MB  432.0 MiB
            28991029248:    29.0 GB   27.0 GiB
          1855425871872:     1.9 TB    1.7 TiB
    9223372036854775807:     9.2 EB    8.0 EiB   (Long.MAX_VALUE)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I convert a PSID type into a byte array that contains the
I just wrote this code to convert a GUID into a byte array. Can
Seeing as you can convert any document to a byte array and save it
How can I convert a Class to byte array in C#. This is a
How can I convert the hashed result, which is a byte array, to a
You can convert a negative number to positive like this: int myInt = System.Math.Abs(-5);
Any tools to convert C code into Java code? I am interested in converting
I'm trying to convert wide char into multi-byte. It's only on yje myfile part.
can someone help to write a method that converts a byte array to a
we can convert character to an integer equivalent to the ASCII value of the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.