unit formatting of negatives
authorMischa POSLAWSKY <perl@shiar.org>
Mon, 31 Oct 2022 19:38:28 +0000 (20:38 +0100)
committerMischa POSLAWSKY <perl@shiar.org>
Tue, 1 Nov 2022 03:14:39 +0000 (04:14 +0100)
barcat
t/t1404-minitude.in [new file with mode: 0644]
t/t1404-minitude_-H.out [new file with mode: 0644]
t/t2201-ascii_-aHl7.out

diff --git a/barcat b/barcat
index 54761d9aaa1835512f2ba4fb8111caf86ed7b2c9..aa9a88e528aa67c5b1c652c6c35a4eb372882f4b 100755 (executable)
--- a/barcat
+++ b/barcat
@@ -114,12 +114,13 @@ $opt{'calc-format'} = sub { sprintf '%*.*f', 0, 2, $_[0] };
 $opt{'value-format'} = $opt{units} && sub {
        my $unit = int(
                log(abs $_[0] || 1) / log(10)
-               - 3 * ($_[0] < .9995)   # shift to smaller unit if below 1
+               - 3 * (abs($_[0]) < .9995)   # shift to smaller unit if below 1
                - log(.9995) / log(10)  # 3 digits rounding up
                + 1e-15  # float imprecision
        );
        my $float = $_[0] !~ /^0*[-0-9]{1,3}$/;
-       sprintf('%3.*f%1s',
+       sprintf('%*.*f%1s',
+               3 + ($_[0] < 0), # digits plus optional negative sign
                $float && ($unit % 3) == ($unit < 0),  # tenths
                $_[0] / 1000 ** int($unit/3),   # number
                $#{$opt{units}} * 1.5 < abs $unit ? "e$unit" : $opt{units}->[$unit/3]
diff --git a/t/t1404-minitude.in b/t/t1404-minitude.in
new file mode 100644 (file)
index 0000000..61a5a77
--- /dev/null
@@ -0,0 +1,26 @@
+-1e-11
+-2e-10
+-3e-09
+-4e-08
+-5e-07
+-6e-06
+-7e-05
+-0.0008
+-0.009
+-0.1
+-1.1
+-12
+-130
+-1400
+-15000
+-160000
+-1700000
+-18000000
+-190000000
+-2000000000
+
+-999499 -
+-999500 +
+-1000000
+-1049999 -
+-1050000 +
diff --git a/t/t1404-minitude_-H.out b/t/t1404-minitude_-H.out
new file mode 100644 (file)
index 0000000..0c6388b
--- /dev/null
@@ -0,0 +1,26 @@
+ -10p   ----------------------------=+|
+-200p   ----------------------------=+|
+-3.0n   ----------------------------=+|
+ -40n   ----------------------------=+|
+-500n   ----------------------------=+|
+-6.0μ   ----------------------------=+|
+ -70μ   ----------------------------=+|
+-800μ   ----------------------------=+|
+-9.0m   ----------------------------=+|
+-100m   ----------------------------=+|
+-1.1    ----------------------------=+|
+ -12    ----------------------------=+|
+-130    ----------------------------=+|
+-1.4k   ----------------------------=+|
+ -15k   ----------------------------=+|
+-160k   ----------------------------=+|
+-1.7M   ----------------------------=+|
+ -18M   ----------------------------=+|
+-190M   ----------------------------
+-2.0G   
+
+-999k - ----------------------------=+|
+-1.0M + ----------------------------=+|
+-1.0M   ----------------------------=+|
+-1.0M - ----------------------------=+|
+-1.1M + ----------------------------=+|
index fbae8fc7ee061a550504443a0a029868f894524d..31e871d61a6b904d6ec03a2c4932b1317012efe2 100644 (file)
@@ -1,5 +1,5 @@
 123u micro ------------<-=-+--------|
 100m long> ------------<-=-+--------|-
- -1        ------------<-=-+
- -2        ---------
- -3        
 -1        ------------<-=-+
 -2        ---------
 -3