Perl Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPerl Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
SlickEdit: Code in over 40 languages across 7 platforms. SlickEdit’s unmatched power, speed, and flexibility allows even the most accomplished developers to write better code faster. Download a free trial today!
  #1  
Old November 19th, 2000, 06:57 PM
tbonds
Guest
Dev Shed Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
$tax = sprintf("$%5.2f", tax());

===================================
The above expression returns "5.2f", instead
of formatting the variable contents and returning a value like "$4.50". However, when I get rid of the "$" in the above expression it returns a formatted number, but does not place the dollar sign in front of the number.

Am I missing something here? I've checked a few examples on this site and in my Perl books, but don't see what I'm doing wrong.

Please help!! Thanks.

Reply With Quote
  #2  
Old November 19th, 2000, 07:36 PM
Envex Envex is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2000
Posts: 4 Envex User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0

Hello there,

It's because $ is a special character in Perl. Try this line instead:

$tax = "$" . sprintf("%5.2f", tax());

Hope that helps,
Mike

Reply With Quote
  #3  
Old November 19th, 2000, 08:47 PM
tbonds
Guest
Dev Shed Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
<BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">quote:</font><HR>Originally posted by Envex:

Hello there,

It's because $ is a special character in Perl. Try this line instead:

$tax = "$" . sprintf("%5.2f", tax());

Hope that helps,
Mike
[/quote]

If what you say is true, why don't the Perl books cover your alternate way of representing my statement? Even the DevShed section doesn't mention this. Anyway, thanks for your help.


Reply With Quote
  #4  
Old November 20th, 2000, 01:33 AM
Envex Envex is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2000
Posts: 4 Envex User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0

If what I say is true?!? It's common sense. $ represents a scalar variable, so you can't just go throw it in anywhere.

Mike


------------------
Envex Developments
Your CGI Script Specialists
http://www.envex.net/

Reply With Quote
  #5  
Old November 20th, 2000, 09:11 AM
tbonds
Guest
Dev Shed Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
So, printf("$%2.2f", 25.99) works OK, but sprintf("$%2.2f", 25.99) doesn't work OK? I tried your method and was successful. However, why would it work OK with printf, but not sprintf? Is there a difference? Seems inconsistent to me. I'm not doubting what you say, but I'm learning Perl through books and this Website's tutorials, so either there are type-o's in Devshed's tutorials or my Perl compiler is broken/outdated! Every example of the above uses the "$" within the formatting expression, yet I can't seem to use it that way and still get good results with my numbers.

DevShed tutorial regarding the printf and sprintf fuctions can be found at: http://www.devshed.com/Server_Side/Perl/Perl101_6/page6.html

Regards,



Reply With Quote
  #6  
Old November 20th, 2000, 10:39 AM
freebsd
Guest
Dev Shed Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
What you are trying to do already been replied at -> http://www.devshed.com/Talk/Forums/Forum6/HTML/000571.html

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPerl Programming > sprintf function not working "as advertised"


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway