0

I want to use this code to convert bytes into proper values

import java.util.*;
import java.lang.*;
import java.io.*;
import java.text.DecimalFormat;

public class Ideone
{

    public static void main (String[] args) throws java.lang.Exception
    {
        String readableFileSize = readableFileSize(9394343298L);
        System.out.println("Result " + readableFileSize);
    }



    public static String readableFileSize(long size)
    {
            if (size <= 0)
                return "0";
            final String[] units = new String[]
            {
                "B", "kB", "MB", "GB", "TB"
            };
            int digitGroups = (int) (Math.log10(size) / Math.log10(1024));
            return new DecimalFormat("#,##0.#").format(size / Math.pow(1024, digitGroups)) + " " + units[digitGroups];
    }
}

The lode works but I have to place L after the number. In my case I use closed source library which when I use it I can't add L. Is there any way to refactor to code to convert very big values into proper format?

Peter Penzov
  • 1,126
  • 134
  • 430
  • 808

1 Answers1

1

I think you're referring to the "L" here?

    String readableFileSize = readableFileSize(9394343298L);

That's how you construct a constant that's a long type, rather than int type, in Java.

From Primitive Data Types - Integer Literals:

An integer literal is of type long if it ends with the letter L or l; otherwise it is of type int. It is recommended that you use the upper case letter L because the lower case letter l is hard to distinguish from the digit 1.

If you're working with a library, it seems likely that it already gives you a long, or something that can be cast to one. For example, you can "upcast" an int to a long, but you can't safely go the other way around.

See also: Initialize a long in Java


An example of how I'd expect your code to work with a library:

public static void main(String[] args) throws Exception {
    LibraryObject lib = new LibraryObject();
    long someSize = lib.doSomething();
    String readableFileSize = readableFileSize(someSize);
}

But without more details on what this "library" you're using does, what you want to do with it, etc, I can't say for sure.

Community
  • 1
  • 1
pioto
  • 2,472
  • 23
  • 37
  • @PeterPenzov Working example of what? You didn't explain what are you trying to achieve and problem you are facing. If you want to be able to write `readableFileSize(9394343298L);` without `L` then you are out of luck because `L` is mandatory there as symbol of `long` type literal. You can't remove it. – Pshemo Apr 17 '15 at 19:02
  • @PeterPenzov Can't you use String instead? Something like `readableFileSize(Long.parseLong("9394343298"));`? – Pshemo Apr 17 '15 at 19:05
  • @Pshemo I get java.lang.ArrayIndexOutOfBoundsException: 5 – Peter Penzov Apr 17 '15 at 21:06
  • @PeterPenzov Not if you are using code you posted in your question. If this exception is involved with other code then without seeing it I will not be able to help you. – Pshemo Apr 17 '15 at 21:09
  • @Pshemo No this is related to the code that I posted. Can you propose some alternative solution for the problem? – Peter Penzov Apr 17 '15 at 21:11
  • @Pshemo Is there a way to use Long value instead of String for data input? – Peter Penzov Apr 17 '15 at 21:23
  • @PeterPenzov Yes, but if it is greater than max `int` it requires `L` suffix which you don't want to add. Anyway your question seems like [XY problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) so until you update your question with more informations about why you don't want to add `L` suffix I doubt I will be able to help you. – Pshemo Apr 17 '15 at 21:29