用逗号显示下载文件的百分比
Display the percentage of an downloaded file with comma
我想检查我在应用程序中的下载速度,为此我将从服务器下载一个 1GB 的 zip 文件。我无法弄清楚如何用逗号获得 12,4% 或 45,8% 这样的数字。确切的文件大小是 222331664 字节
import java.net.*;
import java.io.*;
public class SpeedTest
{
private static Logger log;
private static URL fileToDownload;
public static void main(String argc[]) throws Exception {
log = new Logger("SpeedTest");
// http://kernel.ubuntu.com/~kernel-ppa/mainline/v2.6.15/linux-headers-2.6.15-020615_2.6.15-020615_all.deb
fileToDownload = new URL("http://www.specialservice.ml/1GB.zip");
log.logToLogger("Lade " + fileToDownload);
long totalDownload = 0; // total bytes downloaded
final int BUFFER_SIZE = 1024; // size of the buffer
byte[] data = new byte[BUFFER_SIZE]; // buffer
BufferedInputStream in = new BufferedInputStream(fileToDownload.openStream());
int dataRead = 0; // data read in each try
long startTime = System.nanoTime(); // starting time of download
while ((dataRead = in.read(data, 0, 1024)) > 0) {
totalDownload += dataRead; // adding data downloaded to total data
float tempPercentage = (totalDownload * 100) / 222331664;
log.logToLogger("lade " + dataRead + " Bytes -> " + String.format("%.2f", tempPercentage) + "% geladen");
}
/* download rate in bytes per second */
float bytesPerSec = totalDownload / ((System.nanoTime() - startTime) / 1000000000);
log.logToLogger(bytesPerSec + " Bps");
/* download rate in kilobytes per second */
float kbPerSec = bytesPerSec / (1024);
log.logToLogger(kbPerSec + " KBps ");
/* download rate in megabytes per second */
float mbPerSec = kbPerSec / (1024);
log.logToLogger(mbPerSec + " MBps ");
}
}
您可以使用String.format,例如:
String.format("%.2f", tempPercentage);
如果我没理解错的话,您想将逗号后的浮点数四舍五入为小数点后一位?如果是这样,试试这个:
float tempPercentage = (totalDownload * 100) / 222331664;
String roundedPercentage = String.format("%.1f, tempPercentage);
log.logToLogger("lade " + dataRead + " Bytes -> " + roundedPercentage + "% geladen");
%.1f
中的 1
是您希望在逗号后保留多少位小数,因此如果您稍后决定在逗号后保留 2 位小数,只需更改此 %.2f
。
DecimalFormat df = new DecimalFormat();
DecimalFormatSymbols symbols = new DecimalFormatSymbols();
symbols.setDecimalSeparator(',');
df.setDecimalFormatSymbols(symbols);
log.logToLogger("lade " + dataRead + " Bytes -> " + ´df.format(tempPercentage) + "% geladen");
如果您需要更灵活的格式,可以使用DecimalFormat。
您需要将 222331664
字面量设为 float
float tempPercentage = (totalDownload * 100) / 222331664f;
并使用以下格式对其进行格式化:
String.format("%.1f", tempPercentage).replace(".", ",");
我想检查我在应用程序中的下载速度,为此我将从服务器下载一个 1GB 的 zip 文件。我无法弄清楚如何用逗号获得 12,4% 或 45,8% 这样的数字。确切的文件大小是 222331664 字节
import java.net.*;
import java.io.*;
public class SpeedTest
{
private static Logger log;
private static URL fileToDownload;
public static void main(String argc[]) throws Exception {
log = new Logger("SpeedTest");
// http://kernel.ubuntu.com/~kernel-ppa/mainline/v2.6.15/linux-headers-2.6.15-020615_2.6.15-020615_all.deb
fileToDownload = new URL("http://www.specialservice.ml/1GB.zip");
log.logToLogger("Lade " + fileToDownload);
long totalDownload = 0; // total bytes downloaded
final int BUFFER_SIZE = 1024; // size of the buffer
byte[] data = new byte[BUFFER_SIZE]; // buffer
BufferedInputStream in = new BufferedInputStream(fileToDownload.openStream());
int dataRead = 0; // data read in each try
long startTime = System.nanoTime(); // starting time of download
while ((dataRead = in.read(data, 0, 1024)) > 0) {
totalDownload += dataRead; // adding data downloaded to total data
float tempPercentage = (totalDownload * 100) / 222331664;
log.logToLogger("lade " + dataRead + " Bytes -> " + String.format("%.2f", tempPercentage) + "% geladen");
}
/* download rate in bytes per second */
float bytesPerSec = totalDownload / ((System.nanoTime() - startTime) / 1000000000);
log.logToLogger(bytesPerSec + " Bps");
/* download rate in kilobytes per second */
float kbPerSec = bytesPerSec / (1024);
log.logToLogger(kbPerSec + " KBps ");
/* download rate in megabytes per second */
float mbPerSec = kbPerSec / (1024);
log.logToLogger(mbPerSec + " MBps ");
}
}
您可以使用String.format,例如:
String.format("%.2f", tempPercentage);
如果我没理解错的话,您想将逗号后的浮点数四舍五入为小数点后一位?如果是这样,试试这个:
float tempPercentage = (totalDownload * 100) / 222331664;
String roundedPercentage = String.format("%.1f, tempPercentage);
log.logToLogger("lade " + dataRead + " Bytes -> " + roundedPercentage + "% geladen");
%.1f
中的 1
是您希望在逗号后保留多少位小数,因此如果您稍后决定在逗号后保留 2 位小数,只需更改此 %.2f
。
DecimalFormat df = new DecimalFormat();
DecimalFormatSymbols symbols = new DecimalFormatSymbols();
symbols.setDecimalSeparator(',');
df.setDecimalFormatSymbols(symbols);
log.logToLogger("lade " + dataRead + " Bytes -> " + ´df.format(tempPercentage) + "% geladen");
如果您需要更灵活的格式,可以使用DecimalFormat。
您需要将 222331664
字面量设为 float
float tempPercentage = (totalDownload * 100) / 222331664f;
并使用以下格式对其进行格式化:
String.format("%.1f", tempPercentage).replace(".", ",");