On Jun 15, 10:09 pm, esia...@yahoo.com (William) wrote: > Hello, if I use the sprintf function it would give me a number STRING, but not number, so I have to trick the string to become number by adding and subtracting the number with 1. Is there a better way than this ? I want it to be a number data type and not string. > > e.g > > my $strNumber = sprintf("%04d", 123); > my $number = $strNumber + 1 - 1; # to trick Perl to convert to number datatype instead of string. The only improvement I can see is the rather obvious: my $number = 123; The sprintf() function *always* returns a string (PV). It therefore follows that if you want to convert what sprintf() returned into a number (IV, UV, or NV) then you *must* perform some "trick" on that returned value. You can do it as: my $number = sprintf("%04d", 123) + 1 - 1; # your original approach or my $number = sprintf("%04d", 123) + 0; or my $number = sprintf("%04d", 123) * 1; And there are other similar convoluted approaches available to you. You can possibly even get away with such things as: my $number = exp(log(sprintf("%04d", 123))); # not recommending this :-) The thing that strikes me as strange is this: Given that sprintf() always returns a string, and given that you need a number, why on earth are you using sprintf() in the first place ? (Perhaps it was just for demonstration purposes ?) Cheers, RobThread Previous | Thread Next