如何在Perl中的科学和十进制表示法之间转换? [英] How can I convert between scientific and decimal notation in Perl?

查看:102
本文介绍了如何在Perl中的科学和十进制表示法之间转换?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这是一个新手问题,但是对于许多新程序员来说,答案可能并不明显。最初对我而言并不明显,所以我在Internet上搜索了Perl模块以完成此简单任务。

I know this is a total newbie question, but the answer may not be obvious to many new programmers. It wasn't initially obvious to me so I scoured the Internet looking for Perl modules to do this simple task.

推荐答案

sprintf 可以达到目的

use strict;
use warnings;

my $decimal_notation = 10 / 3;
my $scientific_notation = sprintf("%e", $decimal_notation);

print "Decimal ($decimal_notation) to scientific ($scientific_notation)\n\n";

$scientific_notation = "1.23456789e+001";
$decimal_notation = sprintf("%.10g", $scientific_notation);

print "Scientific ($scientific_notation) to decimal ($decimal_notation)\n\n";

生成以下输出:

Decimal (3.33333333333333) to scientific (3.333333e+000)

Scientific (1.23456789e+001) to decimal (12.3456789)

这篇关于如何在Perl中的科学和十进制表示法之间转换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆