On Thu, Mar 24, 2011 at 16:53, Chris Stinemetz
<[email protected]> 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
}

$dist = ( length( $dist ) > 1 ) ? round($dist/6.6/8/2*10/10, 1) : 0;



-- 
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.

--
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/


Reply via email to