I have yet to find a display format for arbitrary floating point numbers that I like much. I wanted something closer to "Engineering notation" (exponents are always a multiple of 3) but this works for now.

The code is tested but could probably be improved a great deal.

I'd like to be able to make the width a parameter (instead of fixed at 9). Being able to go as low as 7 (or even 6) would be way cool.

#!/usr/bin/perl -w use strict; Main(); exit( 0 ); { my( %fmt4exp, @exps2fmt, $fullfmt ); BEGIN { %fmt4exp= ( -100 => '+1.0e-999', -10 => '+1.00e-99', -5 => '+1.000e-9', -1 => '+0.000000', +3 => ' +0.000', +5 => ' +0.0', +7 => ' +0', +9 => '+1.000e+9', +99 => '+1.000e99', +99999 => '+1.00e999', ); @exps2fmt= sort {$a<=>$b} keys %fmt4exp; $fullfmt= '%+14.7e'; # %+1.2345678e-99 } sub Num2Str { my( $num )= @_; my $full= sprintf $fullfmt, $num; my( $sign, $one, $rest, $esign, $eabs )= $full =~ m< ^\s* ([-+]?)(\d)\.(\d*) [eE]([-+]?)(\d+) \s*$ >x; my $exp= $esign . $eabs; my $fmt; for my $exp2fmt ( @exps2fmt ) { if( $exp <= $exp2fmt ) { $fmt= $fmt4exp{$exp2fmt}; last; } } my $str= $fmt; if( $fmt =~ /e/ ) { $str =~ s/\+/$sign/; $str =~ s/1/$one/; $str =~ s{\.(0+)(?=[eE])}{ ( sprintf( "%14.".length($1)."e", $num ) =~ /(\.\d+)/ )[0] }e; $str =~ s/(9+)/sprintf "%0".length($1)."d", $eabs/e; } else { if( $exp < 0 ) { $str =~ s/\+/$sign/; $rest= '0'x($eabs-1) . $one . $rest; } else { $one= substr( $one . $rest, 0, 1+$exp ); substr( $rest, 0, $exp )= ''; $str =~ s/([\s+0]+)/sprintf "%+".length($1)."s", $sign +.$one/e; } $str =~ s{\.(0+)}{ ( sprintf( "%14.".length($1)."f", $num ) =~ /(\.\d+)$/ )[0] }e; $str =~ s{\.(0+)$}{ ( sprintf( "%14.".length($1)."f", $num ) =~ /(\.\d+)/ )[0] }e; $str =~ s/(\.?0+)$/' ' x length($1)/e if $fmt =~ /\./; } return $str; } } sub Main { for my $exp ( -101..-98, -11, -10..11, 98..101 ) { for my $sign ( '', '-' ) { my $num= 0 + ( $sign . "5.555555555e" . $exp ); printf "%-20s (%s)\n", $num, Num2Str($num); } } for my $exp ( -10..11 ) { for my $sign ( '', '-' ) { my $num= 0 + ( $sign . "1e" . $exp ); printf "%-20s (%s)\n", $num, Num2Str($num); printf "%-20s (%s)\n", 0, Num2Str(0) if 1 == $num; } } }

In reply to Display floating point numbers in compact, fixed-width format by tye

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.