0

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

3
Contributors
6
Replies
7
Views
7 Years
Discussion Span
Last Post by darkagn
1

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.

Votes + Comments
This was the perfect solution!
0

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!

0

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!)

0

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...

Edited by Fbody: n/a

0

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.

0

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.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.