perl - 如何在 Perl 中转换科学记数法和十进制记数法?

标签 perl decimal notation scientific-notation

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

最佳答案

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 - 如何在 Perl 中转换科学记数法和十进制记数法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2474424/

相关文章:

algorithm - 对包含随机数的数组进行排序

algorithm - 这个算法流程符号的名称是什么?

linux - Perl 代码中的/dev/urandom

perl - 可以连接两个使用不同输入记录分隔符的 Perl 脚本吗?

JAVA:使用输入的整数来定义小数位

mysql - DECIMAL类型舍入额外的小数并且不截断mysql

jq - jq 表示法 <function>/<number> 是什么意思?

mysql - 如何绑定(bind)值 INSERT INTO mysql perl

Perl LWP::简单 HTTPS 错误

java - 理解 Java 变量/小数