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

Good afternoon,

I would like to place a question here. I want to convert a decimal number to the scientific notation. I made the following routine, but it truncates the decimals:

sub dec2eng { ($mydecimal)=@_; $myscience = sprintf("%e", $mydecimal); return $myscience; }

For instance: the decimal number 0.99982928833 is converted to 9.998293e-001 - the number is rounded, and so I loose 5 decimals (28833).

Is there a way to do this calculation without recurring to outside perl modules?

Kind regards, Kepler

Replies are listed 'Best First'.
Re: Decimal to Scientific notation
by stevieb (Canon) on Mar 24, 2015 at 13:27 UTC

    You mean like this?

    #!/usr/bin/perl use warnings; use strict; my $dec = "0.99982928833"; my $sci = sprintf("%.11e", $dec); print $sci;

    -stevieb

      Still doesn't work.... Is it my OS?

        Hello kepler,

        In what way does it not work, and what is your OS? In the meantime, here is my guess: Did you perhaps miss the decimal point in stevieb’s solution?

        2:02 >perl -wE "my $dec = 0.99982928833; my $sci = sprintf qq[%11e], +$dec; say $sci;" 9.998293e-001 2:03 >perl -wE "my $dec = 0.99982928833; my $sci = sprintf qq[%.11e], + $dec; say $sci;" 9.99829288330e-001 2:03 >perl -v This is perl 5, version 20, subversion 2 (v5.20.2) built for MSWin32-x +64-multi-thread

        (Windows 8.1, 64-bit, Strawberry Perl.)

        Hope that helps,

        Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

        Copy/paste your exact code you currently have, along with the output.

        Knowing your OS and output of "perl -v" might help too.

        -stevieb

Re: Decimal to Scientific notation
by karlgoethebier (Abbot) on Mar 24, 2015 at 14:28 UTC

    You might also say something like: perl -e 'map{printf("%.11e", $_)} 0.99982928833;'.

    Update: : NB.: This is a working example (note that it compiles), based on what stevieb suggested above - with the same result. Sorry, that i forgot the newline...

    karls-mac-mini:monks karl$ perl -e 'map{printf("%.11e\n", $_)} 0.99982 +928833;' 9.99829288330e-01

    Regards, Karl

    «The Crux of the Biscuit is the Apostrophe»

      Same thing, without map:
      perl -e 'printf "%.11e\n", 0.99982928833' 9.99829288330e-01

        Yes sure toolic, but:

        karls-mac-mini:monks karl$ perl -e 'map{printf("%.11e\n", $_)} (0.9998 +2928833, 0.99982928832);' 9.99829288330e-01 9.99829288320e-01

        ...if so...;-)

        Best regards, Karl

        «The Crux of the Biscuit is the Apostrophe»

Re: Decimal to Scientific notation
by Not_a_Number (Prior) on Mar 24, 2015 at 19:08 UTC

    All the answers given so far are fine if there are 11* digits after the decimal point. But what if the mantissa is longer than 11 digits?:

    printf "%.11e\n", 0.99982928833123; # 9.99829288331e-001

    Still truncated...

    And what if there are considerably fewer digits:

    printf "%.11e\n", 0.999; # 9.9980000000e-001

    Would the OP want all those extra zeroes?

    One solution (admittedly not very elegant):

    my $dec = 0.99982928833123; my $len = length( ( split /\./, $dec )[1] ); printf "%.${len}e\n", $dec; # 9.99829288331230e-001

    * Well, or 12 in this case.