Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

Number digit format question

by dicty (Sexton)
on Feb 08, 2013 at 16:44 UTC ( #1017857=perlquestion: print w/ replies, xml ) Need Help??
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!


Comment on Number digit format question
Re: Number digit format question
by BrowserUk (Pope) 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 (Monsignor) 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.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1017857]
Approved by BrowserUk
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (3)
As of 2014-07-30 22:11 GMT
Find Nodes?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:

    Results (241 votes), past polls