|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
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
|
|||
|
|||
|
$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. |
|
#2
|
|||
|
|||
|
Hello there, It's because $ is a special character in Perl. Try this line instead: $tax = "$" . sprintf("%5.2f", tax()); Hope that helps, Mike |
|
#3
|
|||
|
|||
|
<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. |
|
#4
|
|||
|
|||
|
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/ |
|
#5
|
|||
|
|||
|
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, |
|
#6
|
|||
|
|||
|
What you are trying to do already been replied at -> http://www.devshed.com/Talk/Forums/Forum6/HTML/000571.html
|
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Perl Programming > sprintf function not working "as advertised" |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|