in reply to Splitting String???
Because "." has a special meaning in regular expressions. Try escaping it:
#!/usr/bin/perl print "Content-type: text/html\n\n"; my $latitude = -88.66666; my @values = split(/\./, $latitude); print "$values[0] xxxx $values[1]";
btw, the quotes around $latitude are not necessary.
|
|---|
| Replies are listed 'Best First'. | |||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
Re^2: Splitting String???
by awohld (Hermit) on Apr 26, 2005 at 06:08 UTC | |||||||||||||||||||
What's wrong? Is there a simpler way to do this? Perhaps in a single function?
| [reply] [d/l] | ||||||||||||||||||
by ikegami (Patriarch) on Apr 26, 2005 at 06:23 UTC | |||||||||||||||||||
You forgot join's first argument.
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
or maybe
You can also do it using string manipluation for a little bit more precision:
Example 1:
Example 2:
| [reply] [d/l] [select] | ||||||||||||||||||
by eibwen (Friar) on Apr 26, 2005 at 06:23 UTC | |||||||||||||||||||
Truncating is fairly simple:
If you want to round, that can be done as well (see UPDATE): $number =~ s/(?<=\.)(\d{1,6})(\d).*$/$2>4?$1+1:$1/e;
UPDATE: The truncation code works; however the rounding code does not. Given that 9 would round to 10, which has the potential to be iterative, eg 9.9999999, it would affect both itself and the preceeding character, which obviously has the ability to affect characters on both sides of the decimal point. Preliminary testing has shown that the correct line is more complex than a simple example. Nevertheless, it remains a decent demonstration of /e. As it turns out the rounding code is considerably easier than I had originally thought:
It's essentially the same thing as the truncation code above, save the addition of the += line. Adding 5 to the decimal is a fairly common way to round an int -- I guess I just didn't realize it applied to rounding the 7th decimal place as well. | [reply] [d/l] [select] | ||||||||||||||||||
by holli (Abbot) on Apr 26, 2005 at 07:47 UTC | |||||||||||||||||||
Okay, I want to truncate the decimal places to 6 decimal places ... Is there a simpler way to do this?You mean like this? Note: The substr is there to avoid the rounding of sprintf. You could write sprintf ("%.6f", $declon) but then the value will get rounded. holli, /regexed monk/ | [reply] [d/l] [select] | ||||||||||||||||||