Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert a string to a number (float) in Perl

Tags:

perl

How do I convert a string to number in Perl?

Example:

$str = '10.0500000';
$number = $str * 1;  # 10.05

Is there another standard way to get 10.05 from '10.0500000' instead of multiplying with one?

like image 402
Bala Avatar asked May 14 '12 12:05

Bala


1 Answers

edit 2020: Answer is wrong, see comment below. I am not permitted to delete an accepted answer.


I am answering the literal question. Perhaps you want to format the number for display.

Both expressions

sprintf '%.2f', '10.0500000'
sprintf '%.2f', 10.0500000

return the string 10.05.

like image 99
daxim Avatar answered Nov 10 '22 00:11

daxim