/ / Comment puis-je convertir entre la notation scientifique et la notation décimale en Perl? - perl, décimal, notation, notation scientifique

Comment puis-je convertir entre notation scientifique et notation décimale en Perl? - perl, décimal, notation, notation scientifique

Je sais que c’est une question totale pour les débutants, mais leLa réponse n’est peut-être pas évidente pour beaucoup de nouveaux programmeurs. Ce n’était pas évident pour moi au départ, alors j’ai fouillé sur Internet à la recherche de modules Perl pour effectuer cette tâche simple.

Réponses:

17 pour la réponse № 1

sprintf fait le tour

use strict;
use warnings;

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

print "Decimal ($decimal_notation) to scientific ($scientific_notation)nn";

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

print "Scientific ($scientific_notation) to decimal ($decimal_notation)nn";

génère cette sortie:

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

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

3 pour la réponse № 2

Sur un thème associé, si vous souhaitez convertir entre la notation décimale et notation d'ingénierie (qui est une version de la notation scientifique), le Nombre :: FormatEng Le module de CPAN est pratique:

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