March 2008 Archives

Rounding and int

| | Comments (2)

Came across a curious fact today. Try this out:

$ perl -e "print int(2.07 * 100)"

You get 206 not 207. Same rounding down happens with 2.01 2.03 and 2.05. As perldoc -f int says,

You should not use this function for rounding

One alternative is to use sprintf, for instance:

my $num = sprintf("%.0f",(2.07 * 100)); # returns 207

Or even, from the command line:

perl -e 'printf("%.0f",(2.07 * 100))' # returns 207

Or you could use Math::Round and let that do the thinking for you. ;)

use SOAP::Lite; # +trace
use strict;

my $uri = 'http://www.webserviceX.NET';
my $url = 'http://www.webservicex.net/globalweather.asmx';

my $soap = SOAP::Lite
       -> uri($uri)
       -> on_action( sub { join '/', $uri, $_[1] } )
       -> proxy($url);
       

$soap->autotype(0);
# $soap->readable(1);

my $method = SOAP::Data->name('GetWeather')->attr({xmlns =>$uri});

my $result = $soap->call($method => (
      SOAP::Data->name('CityName')->value('Cambridge'),
      SOAP::Data->name('CountryName')->value('United Kingdom'),)
)->result;

About this Archive

This page is an archive of entries from March 2008 listed from newest to oldest.

January 2008 is the previous archive.

April 2008 is the next archive.

Find recent content on the main index or look in the archives to find all content.

Powered by Movable Type 4.1