You forgot join's first argument.

#!/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

In reply to Re^3: Splitting String??? by ikegami
in thread Splitting String??? by awohld

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.