From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <s.ivanov@proxmox.com>
Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68])
 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
 key-exchange X25519 server-signature RSA-PSS (2048 bits))
 (No client certificate requested)
 by lists.proxmox.com (Postfix) with ESMTPS id 92519635DF
 for <pve-devel@lists.proxmox.com>; Tue, 25 Jan 2022 14:05:27 +0100 (CET)
Received: from firstgate.proxmox.com (localhost [127.0.0.1])
 by firstgate.proxmox.com (Proxmox) with ESMTP id 804302D360
 for <pve-devel@lists.proxmox.com>; Tue, 25 Jan 2022 14:04:57 +0100 (CET)
Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com
 [94.136.29.106])
 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
 key-exchange X25519 server-signature RSA-PSS (2048 bits))
 (No client certificate requested)
 by firstgate.proxmox.com (Proxmox) with ESMTPS id C4A8F2D356
 for <pve-devel@lists.proxmox.com>; Tue, 25 Jan 2022 14:04:56 +0100 (CET)
Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1])
 by proxmox-new.maurer-it.com (Proxmox) with ESMTP id 910EC45C57
 for <pve-devel@lists.proxmox.com>; Tue, 25 Jan 2022 14:04:56 +0100 (CET)
From: Stoiko Ivanov <s.ivanov@proxmox.com>
To: pve-devel@lists.proxmox.com
Date: Tue, 25 Jan 2022 14:04:43 +0100
Message-Id: <20220125130443.1163977-1-s.ivanov@proxmox.com>
X-Mailer: git-send-email 2.30.2
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
X-SPAM-LEVEL: Spam detection results:  0
 AWL 0.256 Adjusted score from AWL reputation of From: address
 BAYES_00                 -1.9 Bayes spam probability is 0 to 1%
 KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment
 SPF_HELO_NONE           0.001 SPF: HELO does not publish an SPF Record
 SPF_PASS               -0.001 SPF: sender matches SPF record
Subject: [pve-devel] [PATCH installer] get_device_desc: display disk size
 with 2 decimal places
X-BeenThere: pve-devel@lists.proxmox.com
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: Proxmox VE development discussion <pve-devel.lists.proxmox.com>
List-Unsubscribe: <https://lists.proxmox.com/cgi-bin/mailman/options/pve-devel>, 
 <mailto:pve-devel-request@lists.proxmox.com?subject=unsubscribe>
List-Archive: <http://lists.proxmox.com/pipermail/pve-devel/>
List-Post: <mailto:pve-devel@lists.proxmox.com>
List-Help: <mailto:pve-devel-request@lists.proxmox.com?subject=help>
List-Subscribe: <https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel>, 
 <mailto:pve-devel-request@lists.proxmox.com?subject=subscribe>
X-List-Received-Date: Tue, 25 Jan 2022 13:05:27 -0000

instead of cutting it off by using int

especially relevant currently with 2 TB disks (people wonder why
it's displayed as 1 TiB - and would be fine with 1.95TiB).

Reported in our community forum:
https://forum.proxmox.com/threads/.103636/

Tested in a VM with a 100 GiB, 20GiB and 2TiB disk

Addtionally added the explicit return ath the end of the if branch,
instead of relying on the last statement not moving.

Signed-off-by: Stoiko Ivanov <s.ivanov@proxmox.com>
---
Alternatively we could also use sprintf without decimal places for rounding
(Math::Round is a debian package of it's own) - if people consider
20.00GiB to not look too pleasing

Thanks to Dominik for insisting to see the code on this and suggesting
to actually improve it :)

 proxinstall | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)

diff --git a/proxinstall b/proxinstall
index ae0e235..a39d405 100755
--- a/proxinstall
+++ b/proxinstall
@@ -2554,16 +2554,16 @@ sub get_device_desc {
     my ($devname, $size, $model) = @_;
 
     if ($size && ($size > 0)) {
-	$size = int($size/2048); # size in MB, from 512B "sectors"
+	$size = int($size/2048); # size in MiB, from 512B "sectors"
 
 	my $text = "$devname (";
 	if ($size >= 1024) {
-	    $size = int($size/1024); # size in GB
+	    $size = $size/1024; # size in GiB
 	    if ($size >= 1024) {
-		$size = int($size/1024); # size in GB
-		$text .= "${size}TiB";
+		$size = $size/1024; # size in TiB
+		$text .= sprintf("%.2f", $size) . "TiB";
 	    } else {
-		$text .= "${size}GiB";
+		$text .= sprintf("%.2f", $size) . "GiB";
 	    }
 	} else {
 	    $text .= "${size}MiB";
@@ -2571,6 +2571,7 @@ sub get_device_desc {
 
 	$text .= ", $model" if $model;
 	$text .= ")";
+	return $text;
 
     } else {
 	return $devname;
-- 
2.30.2