Page 1 of 1

Having problems with casting string from database to float

Posted: Tue Apr 07, 2009 5:28 pm
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?

Re: Having problems with casting string from database to float

Posted: Tue Apr 07, 2009 6:30 pm
by Christopher
How is the constant MODULE_SHIPPING_USPS_HANDLING defined?

Re: Having problems with casting string from database to float

Posted: Tue Apr 07, 2009 6:38 pm
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.

Re: Having problems with casting string from database to float

Posted: Tue Apr 07, 2009 7:01 pm
by Christopher
Maybe because it is a constant that it is forces to a string. Can you change the database entry to 0.5 ?

Re: Having problems with casting string from database to float

Posted: Tue Apr 07, 2009 7:03 pm
by darthead
I've tried that too. No joy.

Re: Having problems with casting string from database to float

Posted: Tue Apr 07, 2009 11:07 pm
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).

Re: Having problems with casting string from database to float

Posted: Wed Apr 08, 2009 8:57 am
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.