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

  • SEARCH
  • Home
  • 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 8485101
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T20:32:58+00:00 2026-06-10T20:32:58+00:00

I have a bunch of file sizes that I would like to parse. These

  • 0

I have a bunch of file sizes that I would like to parse. These are currently only in GBs. Here are some samples:

  • 1.2GB
  • 2.4GB

I think I should store my byte filesizes in a Long value but I can’t seem to figure it out. Here’s how I’m doing it:

System.out.println(Float.parseFloat("1.2GB".replace("GB", ""))* 1024L * 1024L * 1024L);

This returns a Float value which is displayed as 1.28849024E9. How can I get a Long representation of the filesize in bytes.

I’ve gotten a little confused with the numeric datatypes. Thanks.

  • 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-06-10T20:32:59+00:00Added an answer on June 10, 2026 at 8:32 pm

    Use BigDecimal instead:

    BigDecimal bytes = new BigDecimal("1.2GB".replace("GB", ""));
    bytes = bytes.multiply(BigDecimal.valueOf(1024).pow(3));
    long value = bytes.longValue();
    

    Which you can put in a method:

    public static long toBytes(String filesize) {
        long returnValue = -1;
        Pattern patt = Pattern.compile("([\\d.]+)([GMK]B)", Pattern.CASE_INSENSITIVE);
        Matcher matcher = patt.matcher(filesize);
        Map<String, Integer> powerMap = new HashMap<String, Integer>();
        powerMap.put("GB", 3);
        powerMap.put("MB", 2);
        powerMap.put("KB", 1);
        if (matcher.find()) {
          String number = matcher.group(1);
          int pow = powerMap.get(matcher.group(2).toUpperCase());
          BigDecimal bytes = new BigDecimal(number);
          bytes = bytes.multiply(BigDecimal.valueOf(1024).pow(pow));
          returnValue = bytes.longValue();
        }
        return returnValue;
    }
    

    And call it like:

    long bytes = toBytes("1.2GB");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a bunch of JavaScript files that I would like to include in
I have a HTML/CSS File laid out with a bunch of areas that need
I have bunch of strings, some of which are fairly long, like so: movie.titles
I would like to write a python script that takes a bunch of swf
I have a bunch of strings that I'm writing to a file: private void
I have a forum at /forum/. My nginx sites-available/ config file has a bunch
If I have a whole bunch of requires in my main app file: require
I have an mxml file with a bunch of functions in the script tag,
I have an EAR file with a bunch of JARs in it, and one
I have to correct an existing C file which has a bunch of format

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.