Perl脚本中的十进制到科学计数法

时间:2018-11-20 11:20:37

标签: perl

我需要将$this->db->where('db_posts_user_id', 'db_followers_following'); $this->db->where('db_followers_user_id', 276); $this->db->order_by('db_posts.db_posts_id', 'DESC'); $this->db->join('db_followers', 'db_followers.db_followers_following = db_posts.db_posts_user_id'); $query2 = $this->db->get('db_posts')->result_array(); 转换为decimal values

源代码:scientific conversion

我的代码(在Internet中引用):

\num{0.000002753}

转换后的输出为:use Number::FormatEng qw(:all); print format_eng(0.000002753); print "\n";

但是我的Excepted输出是:2.753e-6

有人可以提供解决此问题的方法。预先感谢。

1 个答案:

答案 0 :(得分:1)

贷方转到@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}