7

これがまったくの初心者の質問であることは承知していますが、多くの新しいプログラマーにとって答えは明らかではないかもしれません。最初はわかりにくかったので、この単純な作業を行うための Perl モジュールをインターネットで探しました。

4

2 に答える 2

18

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)
于 2010-03-19T01:21:42.123 に答える
3

関連するテーマで、10 進数表記と工学表記(科学表記のバージョン) の間で変換したい場合は、CPAN のNumber::FormatEngモジュールが便利です。

use Number::FormatEng qw(:all);
print format_eng(1234);     # prints 1.234e3
print format_pref(-0.035);  # prints -35m
unformat_pref('1.23T');     # returns 1.23e+12
于 2010-03-19T02:27:21.743 に答える