Hi guys I'm having sometrouble here! Please help if you can!

I'm storing in a mysql database the price of some products. I am storing them in cents ie. €5.99 = 599.

I have the following code that will output just that:

<?php $x='599';
			$num = $x;
			$last_num = strlen($num) - 2;
			$y = substr_replace($num, '.', $last_num, 0); ?>

This gives me just what I want.

The thing is I am using paypal's IPN. So I need to check that the value being sent back from paypal is the same value I have in my database.

So from paypal I get:

<?php 
mc_gross = 5.99
?>

so when I do my check to see if they are equal I get a return "False". I presume I would have to change my above function.

Can someone tell me how to go about getting these to be correct?

Thanks

Your $x is a string, you should really have it be an integer. Then, all you would have to do is divide by 100.

If you need a string for some reason, you could try to use intval() to extract an integer value from the string. You still would have to divide by 100 to shift the decimal though.

commented: This was the perfect solution! +1

Ha perfect. I don't know qhy it never occured to me to divide by 100. I just went off and made things far more complicated than needed!

Thanks a million!

Your $x is a string, you should really have it be an integer. Then, all you would have to do is divide by 100.

If you need a string for some reason, you could try to use intval() to extract an integer value from the string. You still would have to divide by 100 to shift the decimal though.

Two things: firstly, php is loosely typed, so if a string happens to be an integer or floating point number you can use it as one, and secondly, if you simply divide 599 by 100 I think you will be surprised by the result (5 not 5.99!)

Two things: firstly, php is loosely typed, so if a string happens to be an integer or floating point number you can use it as one, and secondly, if you simply divide 599 by 100 I think you will be surprised by the result (5 not 5.99!)

That sounds like an implementation error....

The division operator ("/") returns a float value unless the two operands are integers (or strings that get converted to integers) and the numbers are evenly divisible, in which case an integer value will be returned.

I'll give you the string conversion, I was not aware of that, so I make a conscious effort to avoid the situation. But according to this, 599 / 100 should return 5.99 because they are not evenly divisible. I've never had an issue with that not working...

Another thought, if this does prove to be an issue. You could multiply by 0.01 instead of dividing by 100. It's the same net effect, but it should guarantee a float value.

But according to this, 599 / 100 should return 5.99 because they are not evenly divisible. I've never had an issue with that not working...

No, 599 is an integer, 100 is an integer, so the result of the division is an integer. 599/100=5, 599%100=99, 599/100.0=5.99.

Another thought, if this does prove to be an issue. You could multiply by 0.01 instead of dividing by 100. It's the same net effect, but it should guarantee a float value.

Yes that's correct, as is dividing by 100.0, or changing 599 to 599.0.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.