Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: number comparison with a twist

by parv (Vicar)
on Mar 02, 2020 at 14:00 UTC ( #11113631=note: print w/replies, xml ) Need Help??


in reply to number comparison with a twist

Below does not apply to other example data posted later.


What about doing the comparison after converting the string to integer AFTER removing the decimal point ($x =~ s,\.,, ; $x = int( $x ) ; $x == $y ? ... : ... ;)? Won't that work?

# Uses addition to 0 instead of int() to force conversion to number fr +om string. use strict; use warnings; my @compare = ( [ 1990, '19.90' ], [ 465, '4.65' ] ); for my $pair ( @compare ) { my $from_api = $pair->[1]; $from_api =~ s,\.,, ; $from_api += 0; printf "%0.f =?= %0.f => %s\n", $from_api, $pair->[0], ( $from_api == $pair->[0] ? 'same' : 'different' ) ; }

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://11113631]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (2)
As of 2020-10-24 09:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My favourite web site is:












    Results (242 votes). Check out past polls.

    Notices?