convert string with comma into an integer

K

kiwi

hi,
i need to convert a string like this 4,674 into an interger, but
couldn't figure out how to do this.
without using of Regexp.

any help will be appriciated.
 
J

John Bokma

kiwi said:
hi,
i need to convert a string like this 4,674 into an interger, but
couldn't figure out how to do this.
without using of Regexp.

And the problem is? Doing it without a regexp? Why?
 
K

kiwi

John said:
And the problem is? Doing it without a regexp? Why?

Since i'm getting error: can't locate Regexp/Common.pm . i guess that
the machine that i'm using doesn't have this module...?
 
G

Gunnar Hjalmarsson

kiwi said:
i need to convert a string like this 4,674 into an interger, but
couldn't figure out how to do this without using of Regexp.

(my $int = '4,674') =~ tr/,//d;

See "perldoc perlop".
 
J

Jürgen Exner

kiwi said:
i need to convert a string like this 4,674 into an interger, but
couldn't figure out how to do this.

Insufficient information.You didn't even tell us what result you expect:
- do you want to round? This would yield 5 in your example but it would
yield 4 for e.g. 4,123
- or do you want take just the integer portion, e.g. 4 in your example and
for 4,123. Then there is still the question what you want to happen for
negative numbers, e.g. -4,674. Do you want the integer portion (-4) or the
next lower number (-5).

Anyway, "perldoc -q round" will probably get you started.

jue
 
U

usenet

Is this comma a thousands-separator (as is common in the U.S.) or a
fractional (decimal) separator (as is common in Europe)?
 
K

kiwi

Is this comma a thousands-separator (as is common in the U.S.) or a
fractional (decimal) separator (as is common in Europe)?

the thousands-separator... anyway, it works now. thanks for the help.
sorry for not giving more accurate definition.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top