惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

小众软件
小众软件
博客园 - Franky
罗磊的独立博客
G
Google Developers Blog
The GitHub Blog
The GitHub Blog
P
Proofpoint News Feed
Recent Announcements
Recent Announcements
V
V2EX
F
Fortinet All Blogs
阮一峰的网络日志
阮一峰的网络日志
Blog — PlanetScale
Blog — PlanetScale
月光博客
月光博客
U
Unit 42
GbyAI
GbyAI
A
About on SuperTechFans
WordPress大学
WordPress大学
Engineering at Meta
Engineering at Meta
雷峰网
雷峰网
Microsoft Azure Blog
Microsoft Azure Blog
Martin Fowler
Martin Fowler
D
DataBreaches.Net
The Cloudflare Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
MongoDB | Blog
MongoDB | Blog

博客园 - 炎峰森林影

安装 OpenClaw(PowerShell) 安装卸载 WSL 基于Windows,Docker用法 [SSL]免费SSL证书,自动续期 [MySQL]递归生成数据行 解决CSDN不登录就不能复制代码的问题 [JS]判断值为空 [BAT]批量提交到Git [网络搭建]Centos8 + Docker-CE + K8S(kubeadm) [Vue]对Vue,Vuex.store,mixins理解 [Android]keytool创建密钥 [JS]JavaScript this 的六道坎 [MSSQL]跨数据库查询 [Tensorflow]统计功效解析 [Tensorflow]设置双击打开*.ipynb文件 [MSSQL]刷新全部视图 [Python]if-else的多种简洁的写法 [Python]Python __iter__ 深入理解 [Go]VSCode安装GoLang插件依赖安装失败
[Python, PHP]存储单位转换
炎峰森林影 · 2025-04-28 · via 博客园 - 炎峰森林影

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";
}

已知的存储单位

以下是目前定义的存储单位,从最小到最大:

  1. B:字节(Byte)

  2. KB:千字节(Kibibyte,2^10 字节)

  3. MB:兆字节(Mebibyte,2^20 字节)

  4. GB:吉字节(Gibibyte,2^30 字节)

  5. TB:太字节(Tebibyte,2^40 字节)

  6. PB:拍字节(Pebibyte,2^50 字节)

  7. EB:艾字节(Exbibyte,2^60 字节)

  8. ZB:泽字节(Zebibyte,2^70 字节)

  9. YB:尧字节(Yobibyte,2^80 字节)

  10. BB:布字节(Brontobyte,2^90 字节)

  11. NB:诺字节(Nebibyte,2^100 字节)

  12. DB:达字节(Dobibyte,2^110 字节)

是否还有更大的单位?

目前,DB(达字节) 是已知的最大存储单位。再往上,虽然理论上可以定义更大的单位,但尚未被正式命名或广泛使用。例如:

  • CB(Corbibyte,2^120 字节):这是一个理论上的单位,但尚未被国际单位制(SI)或国际电工委员会(IEC)正式定义。

  • QB(Quettabyte,2^130 字节):同样是一个理论上的单位,目前也未被正式定义。