Java tutorial
//package com.java2s; import java.math.RoundingMode; import java.text.DecimalFormat; public class Main { private static DecimalFormat df = new DecimalFormat("#0.00"); /** * @param currentSpeed speed int byte per second * @return String with unit such as 3.2Gb/s 200Mb/s ,... */ public static String getSpeedInAutoUnit(double currentSpeed) { String temp = "0.0 B/s"; df.setRoundingMode(RoundingMode.HALF_UP); if (currentSpeed / (1024 * 1024 * 1024) > 1) {//GB temp = df.format(currentSpeed / (1024 * 1024 * 1024)) + "GB/s"; } else if (currentSpeed / (1024 * 1024) > 1) {//MB temp = df.format(currentSpeed / (1024 * 1024)) + "MB/s"; } else if (currentSpeed / (1024) > 1) {//Kb temp = df.format(currentSpeed / (1024)) + "KB/s"; } else if (currentSpeed > 0) {//b temp = df.format(currentSpeed / (1024)) + "B/s"; } return temp; } }