Sptintf changing money value

Have a piece of code I have been using forever, and actually it was written by Stymiee (his authnet code), has been running flawlessly on several websites but last night something went boom and it screwed up.

Simply put the code does nothing more than ensure the dollar amount sent to them is formatted with two places past the decimal point:

sprintf("%01.2f", $policy->get_policy_price())

I re-tested it and printed out the values to check like this:

echo 'policy price: ' . $policy->get_policy_price() . ' to authnet ' . sprintf("%01.2f", $policy->get_policy_price());

The policy price shows 1625.00 but the price after the sprintf is showing 1.00 (and that’s what was sent to authnet)

The price is put into the object as such:

$price = number_format(round($_POST['insured_acres_n'] * $row['rate_per_acre'], 2), 2);


In this particular circumstance the insured_acres_n amount is 13000 and the rate_per_acre amount is 0.125 so doing the math it is indeed 1625.00 (which the application and order confirmation receipt (ours) is showing yet the amount sent to them is coming out $1.

Any ideas? Is there a bug in sprintf with newer versions of PHP? BTW, running version 5.2.6

What’s odd is this site has run at least 200 orders through with no problem at all … the only difference between them and this one is this one is over $1000

Well I just stumbled upon the issue. It was the number_format when the dollar amount was put into the object. I did these two tests:

echo 'to authnet ' . sprintf("%01.2f", number_format(round(13000 * 0.125, 2), 2));

which returns 1.00

but this one without the number format:

echo 'to authnet ' . sprintf("%01.2f", round(13000 * 0.125, 2), 2);

returns the correct amount, 1625

So just an FYI, still don’t know why it’s happening but it is and others should be aware of it.

What is odd is this code:

echo 'to authnet ' . sprintf("%01.2f", number_format(125, 2));

and this code:

echo 'to authnet ' . sprintf("%01.2f", number_format(125.00, 2));

works as expected … the value is 125.00 :shifty:

EDIT: You found your error :smiley:

Keep in mind that Number_format puts delimiters in for 1000’s places!
“1,000” when used as a number will be translated as 1.

Yeah DUH and that’s why it doesn’t do it on numbers under 1000 :blush:

you can have number_format leave out the thousands separator like this:

$price = number_format(round($_POST['insured_acres_n'] * $row['rate_per_acre'], 2), 2, '.', '');