in reply to Perl with precision
These numbers (ie: strings) may come from anywhere such as a database, a spread sheet etc What is the best way to clean them, add them, multiply them with confidence? :)
Your problem is artificial because you could never get values in the form you show other than by hardcoding them in the source code:
C:\test>perl -E"my $a = 000.76; my $b = 4; my $c = $a * $b; say $c;" 304
If they had come from an external source, then they would be actual strings, and be handled correctly:
C:\test>perl -E"my $a = '000.76'; my $b = 4; my $c = $a * $b; say $c;" 3.04
I'm not actually sure what is happening in the first case, though I suspect it has something to do with so-called "Version strings" (aka. v-strings) (maybe in combination with octal? And possibly a bug?), but the situation only arises with values hard-coded into the source, not when read from external sources.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Perl with precision (octal)
by tye (Sage) on Feb 29, 2012 at 00:04 UTC | |
by lidden (Curate) on Feb 29, 2012 at 00:56 UTC | |
|
Re^2: Perl with precision
by Marshall (Canon) on Feb 29, 2012 at 00:28 UTC | |
by BrowserUk (Patriarch) on Feb 29, 2012 at 00:38 UTC | |
by AlienSpaces (Initiate) on Feb 29, 2012 at 02:01 UTC | |
by BrowserUk (Patriarch) on Feb 29, 2012 at 03:45 UTC | |
by Marshall (Canon) on Feb 29, 2012 at 07:26 UTC | |
by AnomalousMonk (Archbishop) on Feb 29, 2012 at 02:31 UTC | |
by Marshall (Canon) on Feb 29, 2012 at 00:56 UTC |