Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:126360 X-Original-To: internals@lists.php.net Delivered-To: internals@lists.php.net Received: from php-smtp4.php.net (php-smtp4.php.net [45.112.84.5]) by qa.php.net (Postfix) with ESMTPS id 839FC1A00BC for ; Sun, 9 Feb 2025 13:45:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1739108540; bh=KzurAQ5AZJZkKX0vjWMxAve5OlAmp8SnpjMyJFoP6Q4=; h=Date:Subject:To:Cc:References:From:In-Reply-To:From; b=N3qFFNsQm/fnbGy6QArHkNmSJ9cXRKEuEheUqYUl5Dp7xPrVfpu9BKV2MuJNpKnjU ceCFj4JXSGzabxIjSoJxCUosAJEyalkDMY1g+kQeK+m7ZE/s6C5OCvA/sm9rXPqbWY THg38E3ATsEWK9J4WPP1++oO3OOKYxjChOtLua88iwRtZ+TRdEbO/ou78a2Y1WOFXo q1nUUZsJKRW90y05aG8rA2dHGDs2QMGOyL2efmLBVwltNbBmUOXvtADur948J13ZIo Vcl4G6jd+TI2j09Si9ul3i7zIrnIzRE24uTRcxywh04HbDqBgRfn7UgRUHh69Rse5x zLaKblFhaA6jw== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id DCAD9180034 for ; Sun, 9 Feb 2025 13:42:19 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 4.0.0 (2022-12-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-0.2 required=5.0 tests=BAYES_40,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_PASS,SPF_HELO_NONE, SPF_PASS autolearn=no autolearn_force=no version=4.0.0 X-Spam-Virus: No X-Envelope-From: Received: from chrono.xqk7.com (chrono.xqk7.com [176.9.45.72]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Sun, 9 Feb 2025 13:42:19 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bastelstu.be; s=mail20171119; t=1739108701; bh=1V/7vo9jQeUDwfGDaPyvTkhRqGBjHdnPMgmwaX60HL0=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type:from:to:cc:subject:message-id; b=EHJqjrwuhaDEX2iOxOSrIKZFbzXKtt7RnuKzf80rmj3ljJJfdrVMoIJxRjKbcr65X lWlBwnZ/IXkDdd2qbFKgEms7U5kjC0LDA7rKHVOxPg459OElk0Cgn9qga4Y0ItNBhW 05MhZPWqsWBT650GCPa6/dI6pVHnL1MQQTCCG2boT+Z0LwQeStKvH9+cs8nTLUsi3e 7yquLtI4v1BJqSFNPX5XM9qII6bcWl2jxRm7r1CYuNXcJh2/rYT95NFWGekGWwbNuU FkZw/XDlI/T0AydrtMXA6EwDGP4lOu9h9VFBXkWXnQqewgEbTkzfpDmvXZDuuxWDlq sMWVbpHIw9tKQ== Message-ID: <00af7020-e629-4f8a-a4b1-d843856c8c22@bastelstu.be> Date: Sun, 9 Feb 2025 14:45:00 +0100 Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net x-ms-reactions: disallow MIME-Version: 1.0 Subject: Re: [PHP-DEV] Introduction - Sam Lewis To: Eugene Sidelnyk , Sam Lewis Cc: Rob Landers , PHP Internals References: <8053193b-0637-4db4-825d-56e7746111dd@bastelstu.be> <88486267-f5e5-485b-bce1-e30163b5e703@bastelstu.be> Content-Language: en-US In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit From: tim@bastelstu.be (=?UTF-8?Q?Tim_D=C3=BCsterhus?=) Hi On 2/8/25 20:52, Eugene Sidelnyk wrote: > Maybe there could be another good feature to start with - the function to > format bytes into a human-readable format (for debug purpuses) to have > pretty view of the size (like 1.5GB, or 20MB) > > […] > > This is too little thing for having separate composer library, and perhaps > having it built-in would be better? On a surface level such a function would appear to be very simple, but the “human-readable format” bit alone already raises multiple questions, with the most notable one being: What is human-readable? There's different languages in the world and they all use different decimal separators and group digits differently. And they possibly have different rules with regard to whether or not a space is required between the scalar part and the unit. In fact your example already is *incorrect* English, because English requires a space between the scalar part and the unit. And it should ideally be a non-breaking space. So in English it would be: - 1.5 GB - 20 MB In German we use the comma as the decimal separator (and also a space before the unit), so it would need to be: - 1,5 GB - 20 MB Then there's also the question of whether to use the binary scale or the decimal scale. In other words: Should 1460 Bytes be rendered as 1.5 kB or as 1.4 KiB? Also: How many decimal digits should be printed? Should it even be a fixed number of decimal digits, or are we rather interested in a total number of significant digits? In more explicit terms: For 2 *decimal* digits: 1234 Bytes -> 1.23 kB 12345 Bytes -> 12.35 kB 123456 Bytes -> 123.46 kB For 3 *significant* digits: 1234 Bytes -> 1.23 kB 12345 Bytes -> 12.3 kB 123456 Bytes -> 123 kB Then there's the question of when the next unit should be used. Should the cut-off point be “there needs to be a 1 in front of the decimal point”? In some cases printing 0.9 GB might be preferable to 900 MB. I could probably go on and find further questions, but I believe this already showcases how the devil is in the details - as with many RFCs that look great on a surface level. In this specific case of usefully formatting numbers, I can recommend taking a look at the NumberFormatter class of ext/intl (https://www.php.net/manual/en/class.numberformatter.php). The API is not particularly pretty, but it handles the complex details of “correctly formatting a float according to language rules”. AFAICT you would still need to append the correct unit (and divide the number of bytes) yourself, though. Best regards Tim Düsterhus