[Python, PHP]存储单位转换
Python
import math
def auto_convert(size):
units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB', 'BB', 'NB', 'DB', 'CB', 'QB']
if size == 0: return "0 B"
i = int(math.log(size, 1024))
return f"{size / 1024**i:.2f} {units[i]}"
try:
print(auto_convert(float(input("请输入字节数:"))))
except:
print("输入无效!")
def convert_storage(size, unit):
units = {'B': 1, 'KB': 1024, 'MB': 1024**2, 'GB': 1024**3, 'TB': 1024**4,
'PB': 1024**5, 'EB': 1024**6, 'ZB': 1024**7, 'YB': 1024**8,
'BB': 1024**9, 'NB': 1024**10, 'DB': 1024**11, 'CB': 1024**12, 'QB': 1024**13}
if unit not in units:
raise ValueError(f"Invalid unit. Please use one of {list(units.keys())}.")
return size * units[unit]
# 示例用法
if __name__ == "__main__":
# 字节转换为其他单位
bytes_size = 1024000
target_unit = 'MB'
converted_size = convert_storage(bytes_size, target_unit)
print(f"{bytes_size} Bytes is equal to {converted_size / units[target_unit]:.2f} {target_unit}")
# 其他单位转换为字节
size = 2.5
source_unit = 'GB'
bytes_size = convert_storage(size, source_unit)
print(f"{size} {source_unit} is equal to {bytes_size} Bytes")
PHP
function autoConvert($size) {
$units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB', 'BB', 'NB', 'DB', 'CB', 'QB'];
if ($size == 0) return "0 B";
$i = floor(log($size, 1024));
return sprintf("%.2f %s", $size / pow(1024, $i), $units[$i]);
}
try {
$size = readline("请输入字节数:");
$size = floatval($size);
if ($size < 0) {
throw new Exception("请输入一个非负数!");
}
echo "转换结果:" . autoConvert($size) . "\n";
} catch (Exception $e) {
echo "输入无效!" . $e->getMessage() . "\n";
}
function convertStorage($size, $unit) {
$units = ['B' => 1, 'KB' => 1024, 'MB' => 1024**2, 'GB' => 1024**3, 'TB' => 1024**4,
'PB' => 1024**5, 'EB' => 1024**6, 'ZB' => 1024**7, 'YB' => 1024**8,
'BB' => 1024**9, 'NB' => 1024**10, 'DB' => 1024**11, 'CB' => 1024**12, 'QB' => 1024**13];
if (!array_key_exists($unit, $units)) {
throw new Exception("Invalid unit. Please use one of " . implode(', ', array_keys($units)) . ".");
}
return $size * $units[$unit];
}
try {
// 字节转换为其他单位
$bytes_size = 1024000;
$target_unit = 'MB';
$converted_size = convertStorage($bytes_size, $target_unit);
echo $bytes_size . " Bytes is equal to " . number_format($converted_size / $units[$target_unit], 2) . " $target_unit\n";
// 其他单位转换为字节
$size = 2.5;
$source_unit = 'GB';
$bytes_size = convertStorage($size, $source_unit);
echo $size . " $source_unit is equal to " . $bytes_size . " Bytes\n";
} catch (Exception $e) {
echo "Error: " . $e->getMessage() . "\n";
}
已知的存储单位
以下是目前定义的存储单位,从最小到最大:
-
B:字节(Byte)
-
KB:千字节(Kibibyte,2^10 字节)
-
MB:兆字节(Mebibyte,2^20 字节)
-
GB:吉字节(Gibibyte,2^30 字节)
-
TB:太字节(Tebibyte,2^40 字节)
-
PB:拍字节(Pebibyte,2^50 字节)
-
EB:艾字节(Exbibyte,2^60 字节)
-
ZB:泽字节(Zebibyte,2^70 字节)
-
YB:尧字节(Yobibyte,2^80 字节)
-
BB:布字节(Brontobyte,2^90 字节)
-
NB:诺字节(Nebibyte,2^100 字节)
-
DB:达字节(Dobibyte,2^110 字节)
是否还有更大的单位?
目前,DB(达字节) 是已知的最大存储单位。再往上,虽然理论上可以定义更大的单位,但尚未被正式命名或广泛使用。例如:
-
CB(Corbibyte,2^120 字节):这是一个理论上的单位,但尚未被国际单位制(SI)或国际电工委员会(IEC)正式定义。
-
QB(Quettabyte,2^130 字节):同样是一个理论上的单位,目前也未被正式定义。
个性签名:做要做好,做到不三不四不如不做。


浙公网安备 33010602011771号