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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

formatting floating-point like .xxx 2

Status
Not open for further replies.

Shrp

IS-IT--Management
Jan 9, 2002
82
US
I have a php script that prints out a floating point value that I would like to format with three positions after the decimal point, but no leading zero.

So I'd like a combination of printf ("%0.3f",$var) and ltrim($var,0), but printf ("%0.3f",ltrim($var,0)) seems to ignore the 'ltrim' part.

So for $var = 1, I'd like to output 1.000, for $var = 1/2 -> .500, $var = 1/3 -> .333, etc. Thanks!
 
Here's one solution:
Code:
<?
$vars = array(1,.5,1/3);
foreach ($vars as $var)
  echo ltrim(sprintf("%0.3f",$var),0)."\n";
?>

What you tried:
Code:
printf ("%0.3f",ltrim($var,0))
does the ltrim before the formatting. My code, does the ltrim after the formatting.

Ken
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top