http://www.perlmonks.org?node_id=1017857

dicty has asked for the wisdom of the Perl Monks concerning the following question:

How to change the number such as 256123456 to 2.56E+08 format in Perl?

Thanks a lot!

Dicty

Replies are listed 'Best First'.
Re: Number digit format question
by BrowserUk (Patriarch) on Feb 08, 2013 at 16:47 UTC

    Do you need that exact format or will this do?:

    printf "%g\n", 256123456;; 2.56123e+008

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Number digit format question
by kennethk (Abbot) on Feb 08, 2013 at 17:02 UTC
    To expound on BrowserUk's post, formatting numbers/rounding numbers is usually done using sprintf and printf. For these routines, the details of the output, such as number of zeroes in the exponent, are controlled by the underlying C libraries on your system and are thus platform dependent. If you well and truly want the string literal above, you'll likely need to decompose the number yourself, and use sprintf to format the coefficient and interpolate it all together, e.g. sprintf "%.2fE%+2.2d", $coef, $exp.

    #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.