PinkeyNBrain
IS-IT--Management
Ran into a scenario where I have some fairly small numbers that need to be printed. I'm wanting them to print as a decimal number without any x.xxxe-yyy scientific notation. One catch is that some of the incoming data may have text. Consider
My previous formating routine worked will until $data[3] recently came along.
Now, $data[3] doesn't match because the "e-" is apparently being seen. I've subsequently modified things to
Which works but looks awkward to me. In the pursuit of code that doesn't propagate poor programing, is there a better approach?
Code:
# Value # Want to print
$data[0] = "0.001" ; # 0.001
$data[1] = "Not Available" ; # Not Available
$data[2] = "0.0001" ; # 0.0001
$data[3] = "0.000000001" ; # 0.000000001
$data[4] = "0.1234567891234567" ; # 0.123456789
Code:
# some hard coding below to make it read faster
sub fmt_num {
if ($_[0] =~ /^[\d\.]+$/) {
return sprintf("%0.9f", $_[0])
}
return $_[0] ;
}
Code:
sub fmt_num {
if ($_[0] =~ /^[\d\.]+$/) {
return sprintf("%0.9f", $_[0])
} elsif ($_[0] =~ /^[\d\.]+e\-\d{3}$/) {
return sprintf("%0.9f", $_[0])
}
return $_[0] ;
}