Pregunta

Sé que esto es una cuestión total de novato, pero la respuesta puede no ser obvio para muchos de los nuevos programadores. No fue inicialmente obvio para mí, así que recorrió todo el Internet en busca de módulos Perl para realizar esta tarea sencilla.

¿Fue útil?

Solución

sprintf hace el truco

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";

genera esta salida:

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

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

Otros consejos

En un tema relacionado, si desea convertir entre la notación decimal y notación de ingeniería ( que es una versión de la notación científica), los href="http://search.cpan.org/search?query=Number%3A%3AFormatEng&mode=all" :: FormatEng módulo de CPAN es práctico:

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
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top