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 7561357
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T13:08:34+00:00 2026-05-30T13:08:34+00:00

I am writing a code in which i need filesize and filesize unit. Lets

  • 0

I am writing a code in which i need filesize and filesize unit. Lets say if someone is calling my function he would call it
myfun (3, MB)

Which means the expected file size would be 3 MB. I am thinking to put this “file unit” in an enum

public enum SizeUnit {BYTE, KILOBYTE, MEGABYTE, GIGABYTE}

Now the problem is converting the the given size into bytes. Of course i can do simple math, but i want something more genering. so lets say if somene has called me function like

myfun (3, MEGABYTE)
in my code i want simply 3 * MEGABYTE to convert it into BYTE unit.

I am not sure if this is possible … I am just thinking about one possibility. There could be a method in enum, which takes the its unit and do the calculation? (i don’t want to do the trivial one, there should be something generic enough).

  • 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-30T13:08:35+00:00Added an answer on May 30, 2026 at 1:08 pm

    You can do something like this:

    public enum SizeUnit
    {
      BYTE(1),
      KILOBYTE(1024),
      ...;
    
      private final int bytes; 
      private SizeUnit(final int bytes) { this.bytes = bytes; }
      public int getByteCount() { return this.bytes; }
    }
    

    Then you can do things like:

    3 * KILOBYTE.getByteCount();
    

    or even define a method called getBytes which passes a number. So KILOBYTE.getBytes(3).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a simple code using jQuery, in which I need to call a
I am writing some code in which i need to get a line from
I'm writing code on the master page, and I need to know which child
I'm writing a select query in a C# method which lets client code provide
I am writing a code in which I need to parse a string to
I'm writing some code in which I need to use my own HttpResponse object
I'm writing a code generator which produces Scala output. I need to emulate a
I'm writing an application for Mac. I need some code which generates a hash
I'm writing unit tests for code which uses JDOM to read and write XML.
I'm writing some code which could really do with some simple compile time metaprogramming.

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.