package com.om.utils;

public class FileSizeConverter {
    public static void main(String[] args) {
        String fileSizeString = "48602";
        long fileSizeInBytes = Long.parseLong(fileSizeString);

        String fileSizeInReadableFormat = convertBytes(fileSizeInBytes);
        System.out.println(fileSizeInReadableFormat);
    }

    public static String convertBytes(long fileSize) {
        String[] units = {"B", "KB", "MB", "GB"};

        int unitIndex = 0;
        double size = fileSize;

        while (size >= 1024 && unitIndex < units.length - 1) {
            size /= 1024;
            unitIndex++;
        }

        return String.format("%.2f %s", size, units[unitIndex]);
    }
}
