CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Numbers

 



andy7t
User

Oct 24, 2003, 9:13 AM

Post #1 of 5 (554 views)
Numbers Can't Post

I need to make sure that an input is in a money form: i.e 5.00 or 5.24

So, firstly, needs to have numbers only (and decimal point), 2 numbers after the decimal point.

Perhaps if the user only enters 5, it could add a double 0 to the end?


davorg
Thaumaturge / Moderator

Oct 24, 2003, 9:56 AM

Post #2 of 5 (552 views)
Re: [andy7t] Numbers [In reply to] Can't Post

printf and sprintf are your friends.


Code
$ perl -e 'printf "%.2f\n", 5' 
5.00
$ perl -e 'printf "%.2f\n", 5.123'
5.12
$ perl -e 'printf "%.2f\n", 5.567'
5.57


--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


andy7t
User

Oct 25, 2003, 1:33 AM

Post #3 of 5 (544 views)
Re: [davorg] Numbers [In reply to] Can't Post

Any way of putting that in a variable, because it's for an auction you see- it needs to be stored in a database.

$bidamount=$ perl -e 'printf "%.2f\n", 5.567' ;


davorg
Thaumaturge / Moderator

Oct 25, 2003, 4:57 AM

Post #4 of 5 (543 views)
Re: [andy7t] Numbers [In reply to] Can't Post

Yeah, use sprintf instead of printf. I recommend you take a look at the docs.

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


KevinR
Veteran


Oct 25, 2003, 9:00 PM

Post #5 of 5 (539 views)
Re: [andy7t] Numbers [In reply to] Can't Post

a simple example using sprintf:

$bidamount = sprintf("%.2f", $bidamount);
-------------------------------------------------

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives