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

Getting the sprinf not to round 2

Status
Not open for further replies.

RPrinceton

Programmer
Jan 8, 2003
86
US
Hi Everyone,

I am using the sprintf function to format a number. 1150.00/17.8 is 64.6067415730337 I want the number to be formatted as 64.60, no rounding. The code below rounds.
How do I get the sprintf function not to round? I have toyed with the idea of letting the sprintf function round to a third decimal place and then use a substr function to trim off the third decimal place. This seems somewhat kludgy. Is there a way to get sprintf not to round?
Please advise. Thx in advance.
Regards,
RPrinceton

$mv1 = 1150.00;
$mv2 = 17.8;
$mvAns = $mv1 / $mv2;
$mvFmt = sprintf("%.2f", $mvAns);

prints 64.61
 
Not necessarily the cleanest solution, but it works:
Code:
$mvFmt = sprintf("%.2f", $mvAns-0.005);
 
Code:
$mvFmt = sprintf("%.2f", int($mvAns * 100) / 100 );


Kind Regards
Duncan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top