1

I have a decimal(15,2) field in my MySQL database named 'price'. I would like to do something like this with PHP:

<? 
    $price = $row->price;
    if($price >= 75) { echo 'Free Delivery'; }
?> 

Do I need to turn $price into an integer before doing this? If so, what's the best way to do this? Any help would be much appreciated!

3
  • 1
    did u tried to echo $price; before if statement to check if it looks good? What error do u have? Commented Dec 11, 2013 at 13:51
  • No you don't have to typecast it. Commented Dec 11, 2013 at 13:51
  • ah ha.... fixed it. The shopping cart I'm using was adding the currency symbol before passing it to the view! Should've checked that first. Thanks for your responses! Commented Dec 11, 2013 at 13:55

3 Answers 3

1

you could use explode() function to get the portions of the string before and after the decimal point. http://php.net/manual/en/function.explode.php

$parts = explode('.', $price);
if($parts[0] >= 75){ echo "free delivery"; }
Sign up to request clarification or add additional context in comments.

1 Comment

It's pointless to explode number to recive int. You don't have to do anything with it.
0

The shopping cart I'm using was adding the currency symbol before passing it to the view! Should've checked that first. Thanks for your responses!

Comments

0

I preferr to store prices in smallest units that are used. If the smallest is cent, so why not just to store cents as normal INT? That will solve many problems and will be fast.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.