Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with printing redundant zero 1

Status
Not open for further replies.

tviman

Programmer
Joined
Jul 25, 2002
Messages
2,123
Location
US
I'm having a brain fart today..... I'm extracting data from a database and putting it on a web page. The data has to be formatted with 2 fixed decimal places. My problem is that some of the data is, for example: 12345.50, but on the web page it shows as 12345.5

There's got to be an easy way to do this but I'm just not seeing it. Any help would be greatly appreciated.


Thanks....

PS: I've tried printf and sprintf but these functions don't allow me to print on a web page.
 
tviman said:
PS: I've tried printf and sprintf but these functions don't allow me to print on a web page.

*rolls eyes*

- Miller
 
tviman said:
Like I said, Im having a brain-fart day so I'm open to suggestions....

Sure thing!

Code:
[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]$number[/blue] = [fuchsia]12345.5[/fuchsia][red];[/red]
[black][b]my[/b][/black] [blue]$formatted[/blue] = [url=http://perldoc.perl.org/functions/sprintf.html][black][b]sprintf[/b][/black][/url][red]([/red][red]"[/red][purple]%.2f[/purple][red]"[/red], [blue]$number[/blue][red])[/red][red];[/red]

[url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] [red]"[/red][purple]Formatted = '[blue]$formatted[/blue]'[/purple][red]"[/red][red];[/red]
[gray][i]# Outputs: Formatted = '12345.50'[/i][/gray]

- Miller
 
Told you I was having a bad day....

I was using sprintf "%2f", $number
(notice lack of parens, which works great in a non-web page)

Told you I was having a bad day....

Thanks so much for your help!
 
tviman said:
I was using sprintf "%2f", $number
(notice lack of parens, which works great in a non-web page)

It's not the lack of parenthesis that is the problem, it's the missing . to indicate that you are setting a precision. Without the dot, you are instead setting a minimum width, which will cause any single digit numbers to be left padded with a space.

Glad I could help.

- Miller
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top