How can I convert a string number to a number in Perl?

How can I convert a string to a number in Perl?

  • How would I convert a string holding a number into its numeric value in Perl?

  • Answer:

    You don't need to convert it at all: % perl -e 'print "5.45" + 0.1;' 5.55

Anton at Stack Overflow Visit the source

Was this solution helpful to you?

Other answers

Perl is a context-based language. It doesn't do its work according to the data you give it. Instead, it figures out how to treat the data based on the operators you use and the context in which you use them. If you do numbers sorts of things, you get numbers: # numeric addition with strings my $sum = '5.45' + '0.01'; # 5.46 If you do strings sorts of things, you get strings: # string replication with numbers my $string = ( 44/2 ) x 3; # "22.522.522.5" Perl mostly figures out what to do and it's mostly right. Another way of saying the same thing is that Perl cares more about the verbs than it does the nouns. Are you trying to do something and it isn't working?

brian d foy

I think the author wants a way to ensure that a value is converted to a numeric value. For example, I have the following code that converts to integer values: $val = int($val || 0); This will convert "25" to "25, "000025" to "25", blank space to 0, and the bogus character string "Bozo" to 0. However, this will only handle integers. Is there a way to do the same thing only with floats instead of integers? This way, the string "0000000.25" will convert to .25 and "Hello Dolly0.0" will convert to 0.0? This is useful, for example, if you're reading spreadsheet cells into a database. If you're inputting into an oracle 'Number' field, you want all invalid and blank values to be input as 0.

Bob Thompson

Google lead me here while searching on the same question phill asked (sorting floats) so I figured it would be worth posting the answer despite the thread being kind of old. I'm new to perl and am still getting my head wrapped around it but brian d foy's statement "Perl cares more about the verbs than it does the nouns." above really hits the nail on the head. You don't need to convert the strings to floats before applying the sort. You need to tell the sort to sort the values as numbers and not strings. i.e. my @foo = ('1.2', '3.4', '2.1', '4.6'); my @foo_sort = sort {$a <=> $b} @foo; See http://perldoc.perl.org/functions/sort.html for more details on sort

Norm

Perl really only has three types: scalars, arrays, and hashes. And even that distinction is arguable. ;) The way each variable is treated depends on what you do with it: perl -e "print 5.4 . 3.4;" 5.43.4 perl -e "print '5.4' + '3.4';" 8.8

Rini

Take what I say with a grain of salt, but it seems like you sometimes DO need to convert it, for example, when producing JSON output with floats in it. I never figured out the right way to do it, so I just multiplied my string by one: 1 * $str

user65969

As I understand it int() http://perldoc.perl.org/functions/int.html is not intended as a 'cast' function for designating data type it's simply being (ab)used here to define the context as an arithmetic one. I've (ab)used (0+$val) in the past to ensure that $val is treated as a number.

mccutchm

This is a simple solution: Example 1 my $var1 = "123abc"; print $var1 + 0; Result 123 Example 2 my $var2 = "abc123"; print $var2 + 0; Result 0

porquero

Um, correct me if I'm wrong, but calling sort on an array of floats will produce a different ordering than if those numbers are all converted to strings before being sorted. Thus, Perl's automatic handling is not always enough. I know there's an int() function, but there doesn't seem to be a float() function. What is it called?

phil

I know this has been long answered, but this has been working for me in handling ints and floats from strings: my $val = '4.45'; my $newval = ($val =~ m/^[\d]+(\.[\d]+)?$/)?($val*1):$val; of course, you need to trim your string first.

Jason

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.