Format Bytes
10
Format Bytes
public static String formatBytes(long lBytes)
{
// Declare variables
String strReturn = null;
long lCalc = 0;
DecimalFormat fFormat = new DecimalFormat("###########.##");
String strSuffix = null;
if (lBytes >= Math.pow(2, 40))
{
lCalc = Math.round(lBytes / Math.pow(1024, 4));
strSuffix = new String("TB");
}
else if (lBytes >= Math.pow(2, 30))
{
lCalc = Math.round(lBytes / Math.pow(1024, 3));
strSuffix = new String("GB");
}
else if (lBytes >= Math.pow(2, 20))
{
lCalc = Math.round(lBytes / Math.pow(1024, 2));
strSuffix = new String("MB");
}
else if (lBytes >= Math.pow(2, 10))
{
lCalc = Math.round(lBytes / Math.pow(1024, 1));
strSuffix = new String("KB");
}
else
{
lCalc = lBytes;
strSuffix = new String("Byte");
}
// Format final
strReturn = fFormat.format(lCalc);
strReturn += strSuffix;
return strReturn;
}
{
// Declare variables
String strReturn = null;
long lCalc = 0;
DecimalFormat fFormat = new DecimalFormat("###########.##");
String strSuffix = null;
if (lBytes >= Math.pow(2, 40))
{
lCalc = Math.round(lBytes / Math.pow(1024, 4));
strSuffix = new String("TB");
}
else if (lBytes >= Math.pow(2, 30))
{
lCalc = Math.round(lBytes / Math.pow(1024, 3));
strSuffix = new String("GB");
}
else if (lBytes >= Math.pow(2, 20))
{
lCalc = Math.round(lBytes / Math.pow(1024, 2));
strSuffix = new String("MB");
}
else if (lBytes >= Math.pow(2, 10))
{
lCalc = Math.round(lBytes / Math.pow(1024, 1));
strSuffix = new String("KB");
}
else
{
lCalc = lBytes;
strSuffix = new String("Byte");
}
// Format final
strReturn = fFormat.format(lCalc);
strReturn += strSuffix;
return strReturn;
}






There are currently no comments for this snippet.