#!/usr/bin/perl
my $declon = -88.9999999999999999999;
my $declat = 44.1111111111111111111;
print($declon, "\n"); # -89
print($declat, "\n"); # 44.1111111111111
my @lon = split(/\./, $declon);
my @lat = split(/\./, $declat);
$lon[1] = substr($lon[1], 0, 6);
$lat[1] = substr($lat[1], 0, 6);
my $lat = join('.', @lat);
my $lon = join('.', @lon);
print "$lat\n";
print "$lon";
Again, I removed the useless quotes around $declon and $declat.
As you can see by the two prints I added near the top, -88.9999999999999999999 gets stringified as -89. You're going beyond the precision of a double. This also demonstrates where your algorithm fails. (-89 prints as "-89." instead of "-89.000000".) What you want is
# Round (.5 rounds up)
$num = int($num * 1000000 + 0.5) / 1000000;
or maybe
# Truncate towards 0.
$num = int($num * 1000000) / 1000000;
You can also do it using string manipluation for a little bit more precision:
$lon = sprintf('%.6f', $declon);
$lat = sprintf('%.6f', $declat);
Example 1:
#!/usr/bin/perl
use strict;
use warnings;
my $declon = -88.9999999999999999999;
my $declat = 44.1111111111111111111;
my $lon = int($declon * 1000000 + 0.5) / 1000000;
my $lat = int($declat * 1000000 + 0.5) / 1000000;
printf("%.6f\n", $lon); # -88.999999
printf("%.6f\n", $lat); # 44.111111
Example 2:
#!/usr/bin/perl
use strict;
use warnings;
my $declon = -88.9999999999999999999;
my $declat = 44.1111111111111111111;
printf("$declon\n"); # -89
printf("$declat\n"); # 44.1111111111111
printf("%.6f\n", $declon); # -89.000000
printf("%.6f\n", $declat); # 44.111111
|