ssr1012
ssr1012

Reputation: 2589

Decimal to Scientific Notation

I need to convert decimal values to scientific notation:

Source coding: \num{0.000002753}

My Code (Referred in Internet):

use Number::FormatEng qw(:all);
print format_eng(0.000002753); 
print "\n";

Converted Output is: 2.753e-6

However, my expected output is: 2.75*10^{-6}

Could someone provide the way to sort this issue out?

Upvotes: 0

Views: 133

Answers (1)

ssr1012
ssr1012

Reputation: 2589

Credits goes to @melpomene from the comments on my question:

while($val=~m/\\num{([^{}]*)\}/g)
{
    my $nums = $1;
    my $vals = sprintf('%.2e', $nums) =~ s/[eE]([-+]?)0*(\d+)\z/*10^{$1$2}/r;

    print "OrgVals: $nums\t: ConvVals: $vals\n";
}

OrgVals: 0.000002753    : ConvVals: 2.75*10^{-6}
OrgVals: 0.000004784    : ConvVals: 4.78*10^{-6}
OrgVals: 0.000050934    : ConvVals: 5.09*10^{-5}

Upvotes: 1

Related Questions