From: Mischa POSLAWSKY Date: Mon, 31 Oct 2022 19:38:28 +0000 (+0100) Subject: unit formatting of negatives X-Git-Tag: v1.08~6 X-Git-Url: http://git.shiar.nl/barcat.git/commitdiff_plain/736f0a09c45e37cfef46689640d7fcac30ff9eee unit formatting of negatives --- diff --git a/barcat b/barcat index 54761d9..aa9a88e 100755 --- 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 index 0000000..61a5a77 --- /dev/null +++ b/t/t1404-minitude.in @@ -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 index 0000000..0c6388b --- /dev/null +++ b/t/t1404-minitude_-H.out @@ -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 + ----------------------------=+| diff --git a/t/t2201-ascii_-aHl7.out b/t/t2201-ascii_-aHl7.out index fbae8fc..31e871d 100644 --- a/t/t2201-ascii_-aHl7.out +++ b/t/t2201-ascii_-aHl7.out @@ -1,5 +1,5 @@ 123u micro ------------<-=-+--------| 100m long> ------------<-=-+--------|- - -1 ------------<-=-+ - -2 --------- - -3 + -1 ------------<-=-+ + -2 --------- + -3