Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: number comparison with a twist

by parv (Parson)
on Mar 02, 2020 at 14:00 UTC ( [id://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
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11113631]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (7)
As of 2024-04-16 07:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found