Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
I wanted something closer to "Engineering notation".... 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.

this code should meet those requirements. at least, the mantissa width is fixed, the exponent width varies. it should be easy enough to modify this code to suit your needs, if you wish.

i used your test suite, too.

#!/usr/bin/perl use strict; use warnings; $|++; Main(); exit; ## adapted from code found at: http://www.cs.tut.fi/~jkorpela/c/eng.ht +ml sub eng { my( $num, $digits )= @_; ## default to smallest number of digits allowing fixed width manti +ssa (4) $digits= defined $digits && 3 < $digits ? $digits : 4; my $neg; if( 0 > $num ) { $neg= 'true'; $num= -$num; } 0 == $num and return sprintf '+%.*fe+%s' => $digits - 1, $num, 0; my $exp= 0 != $num ## perl's log() is natural log, convert to common log ? int( log($num) / log(10) ) ## short-circuit: can't do log(0) : 0; ## tricky integer casting ahead... $exp= 0 < $exp ? int( ( int( $exp / 3 ) ) * 3 ) : int( int( ( -$exp + 3 ) / 3 ) * -3 ); $num *= 10 ** -$exp; if( 1000 <= $num ) { $num /= 1000; $exp += 3; } elsif( 100 <= $num ) { $digits -= 2; } elsif( 10 <= $num ) { $digits -= 1; } 0 <= $exp and $exp= '+' . $exp; return ( $neg ? '-' : '+' ) . sprintf '%.*fe%s' => $digits - 1, $num, $exp; } sub Main { my $digits= 2; 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, eng( $num, $digits ); } } for my $exp ( -10..11 ) { for my $sign ( '', '-' ) { my $num= 0 + ( $sign . "1e" . $exp ); printf "%-20s (%s)\n", $num, eng( $num, $digits ); printf "%-20s (%s)\n", 0, eng( 0, $digits ) if 1 == $num; } } }

~Particle *accelerates*


In reply to Re: Display floating point numbers in compact, fixed-width format by particle
in thread 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!
  • 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, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others wandering the Monastery: (8)
    As of 2014-12-29 17:07 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      Is guessing a good strategy for surviving in the IT business?





      Results (194 votes), past polls