Having problems with casting string from database to float

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
darthead
Forum Newbie
Posts: 6
Joined: Tue Apr 07, 2009 5:15 pm

Having problems with casting string from database to float

Post by darthead »

It seems so simple. I'm trying to evaluate whether a number stored in a MySQL db is smaller than zero. The number is stored a 'text' value in the db, but is representing a float, specifically, a percentage. Here's my code:

Code: Select all

 
$handling = MODULE_SHIPPING_USPS_HANDLING;
$cost_handling = $handling < 0 ? $cost + ($handling * $cost) : $cost + $handling;
 
MODULE_SHIPPING_USPS_HANDLING = .5 in the db.

I've tried casting it as floatval(MODULE_SHIPPING_USPS_HANDLING), (float)MODULE_SHIPPING_USPS_HANDLING, adding a float to it, and many others but to no avail. The code acts as if it's a string (I think) and $cost_handling always evaluates to the false side of the operator ($handling is added correctly to $cost to produce a decimal value).

What am I doing wrong?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Having problems with casting string from database to float

Post by Christopher »

How is the constant MODULE_SHIPPING_USPS_HANDLING defined?
(#10850)
darthead
Forum Newbie
Posts: 6
Joined: Tue Apr 07, 2009 5:15 pm

Re: Having problems with casting string from database to float

Post by darthead »

It's stored in the database as 'text', along with all the other configuration options (this is Zen Cart). I assume the bootstrap just reads all the options and defines them as they are. Or do I not understand what you're asking? I'm a little addled here.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Having problems with casting string from database to float

Post by Christopher »

Maybe because it is a constant that it is forces to a string. Can you change the database entry to 0.5 ?
(#10850)
darthead
Forum Newbie
Posts: 6
Joined: Tue Apr 07, 2009 5:15 pm

Re: Having problems with casting string from database to float

Post by darthead »

I've tried that too. No joy.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Having problems with casting string from database to float

Post by Christopher »

I tried it and I think the problem is with the condition ($handling < 0). I don't see how $handling will every be less than zero. I am guessing that if it is less than 1.0 you are using it as a percentage, but if greater than 1.0 then it is an amount. So assume you meant tsomething like ($handling < 1.0).
(#10850)
darthead
Forum Newbie
Posts: 6
Joined: Tue Apr 07, 2009 5:15 pm

Re: Having problems with casting string from database to float

Post by darthead »

OMG! You are SO right. A thousand thank you's!

Of course. Basic math would have helped here. I'm so embarrassed...I literally spent hours on this.

Thank you so much.
Post Reply