Chas. Owens wrote:
On Thu, Mar 24, 2011 at 16:53, Chris Stinemetz
<cstinem...@cricketcommunications.com> wrote:
I have a ternary operator that I would like to be rounded to the nearest tenth
decimal
place before the array is pushed.
The proper term is conditional operator, even in C.
Rounding is a tricky subject (see
http://en.wikipedia.org/wiki/Rounding for the gory details), that is
why Perl 5 does not provide a simple rounding function for you. Do
you have a rounding method in mind, or you just happy with whatever
you get?
snip
$dist =
( length( $dist )> 1 ) ? $dist/6.6/8/2*10/10 : 0 ; ##### I
would like to round the number to the nearest tenth right here. ########
snip
As for how to combine a function call with a conditional, it is simple:
sub round {
my ($n, $places) = @_;
return sprintf "%.${places}f", $n; #I don't care how the number is rounded
Usually written as:
return sprintf '%.*f', $places, $n; #I don't care how the number
is rounded
}
$dist = ( length( $dist )> 1 ) ? round($dist/6.6/8/2*10/10, 1) : 0;
John
--
Any intelligent fool can make things bigger and
more complex... It takes a touch of genius -
and a lot of courage to move in the opposite
direction. -- Albert Einstein
--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/