Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Oct 2007
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Formatting of Numbers - Need good reference material

    I just ran into a problem, that I was finally able to overcome, but the solution was not very elegant.

    I have a script that processes input from a HTML form. All of the numeric data is input through text boxes. These values are written to a SQL db where the field definition is "DECIMAL 10,2". I want to use one of these values in a calculation BEFORE I update my db with this value. The problem I run into is this:
    I want to multiply one of the input values by a value from another db table (also defined as DECIMAL 10,2). After I retrieve the second value and echo the numbers to the screen they look like this:

    '13.30' ------->value that was input through text box before being put in db
    2.05 --------->value retrieved from other table for this calculation

    attempting to multiply these two values yields a result of '0'.

    If I do this multiplication AFTER the inputted value has been written to the db and retrieved again then the echo output to the screen looks like this:

    13.30
    2.05

    and the multiplication yields the correct result.

    Falling back on my old Fortran IV days (long, long time ago), this looks like a mixed mode expression, multiplying a string by a numeric variable. Keeping this in mind, I tried several different conversions to try to convert this to decimal, but was never able to come up with the right format. My solution was to just write that input string variable to the db and immediately retrieve it, which yielded the correct format.

    After this longwinded introduction, I guess the basic question is:
    If my assumption that the input data from the form is a character string is correct, how do I massage it so that it can be used in the multiplication?

    Thanks in advance........

  • #2
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    If my assumption that the input data from the form is a character string is correct, how do I massage it so that it can be used in the multiplication?
    yes, is string type.
    you can try to use gettype for checking and floatval for conversion:

    PHP Code:
    $stringvar "12.34";

    print 
    gettype($stringvar);

    $floatvar floatval($stringvar);

    print 
    gettype($floatvar); 
    how, is up to you but I supose this is not a problem.

    happy holydays.

  • #3
    New to the CF scene
    Join Date
    Oct 2007
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you for the answer. I tried that, and came up with some funny results. Here is the code I inserted to test this:

    Code:
    		$my_price = $db->mySQLSafe($_POST['my_price']);
    
    		print gettype($my_price)."<br>";
    		echo $my_price ."<br>";
    		$floatvar = floatval($my_price);
    		print gettype($floatvar)."<br>";
    		Print $floatvar ."<br>";
    The output from the above was:
    string
    '13.30'
    double
    0

    What is even stranger, if I modify that code to look like this:
    // $my_price = $db->mySQLSafe($_POST['my_price']);
    Code:
           $my_price="13.30";
    		print gettype($my_price)."<br>";
    		echo $my_price ."<br>";
    		$floatvar = floatval($my_price);
    		print gettype($floatvar)."<br>";
    		Print $floatvar ."<br>";
    Then I get:
    string
    '13.30'
    double
    13.30

    Being pretty much a newbie to php (even though I retired after many years programming operating systems in ASM and seismic applications in FORTRAN IV), I am thoroughly confused. Please, someone turn on the lights......

  • #4
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by CaseyC View Post
    Thank you for the answer. I tried that, and came up with some funny results. Here is the code I inserted to test this:

    Code:
    		$my_price = $db->mySQLSafe($_POST['my_price']);
    
    		print gettype($my_price)."<br>";
    		echo $my_price ."<br>";
    		$floatvar = floatval($my_price);
    		print gettype($floatvar)."<br>";
    		Print $floatvar ."<br>";
    The output from the above was:
    string
    '13.30'
    double
    0

    What is even stranger, if I modify that code to look like this:
    // $my_price = $db->mySQLSafe($_POST['my_price']);
    Code:
           $my_price="13.30";
    		print gettype($my_price)."<br>";
    		echo $my_price ."<br>";
    		$floatvar = floatval($my_price);
    		print gettype($floatvar)."<br>";
    		Print $floatvar ."<br>";
    Then I get:
    string
    '13.30'
    double
    13.30
    the only difference between this is the use of mySQLSafe, so the problem must be in mySQLSafe. I didn't find it in php manual

    http://www.php.net/manual/en/

    I presume that is a wrapper to mysql but I don't know this module, I search the net and I didn't find it, it must be somewhere on your system, maybe if we are looking inside we can figure out what is wrong.

    Being pretty much a newbie to php (even though I retired after many years programming operating systems in ASM and seismic applications in FORTRAN IV), I am thoroughly confused. Please, someone turn on the lights......
    I was starting with fortran iv too, Try to tell somebody now that you must deal with 256 bytes of RAM

    best regards, happy holidays

  • #5
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,642
    Thanks
    2
    Thanked 405 Times in 397 Posts
    You don't need the mySQLsafe function if you're typecasting it to a float or int. You can also typecast like this:
    PHP Code:
    $test '12.34';
    $test = (float) $test
    Quote Originally Posted by oesxyl View Post
    I was starting with fortran iv too, Try to tell somebody now that you must deal with 256 bytes of RAM
    Bytes? That would be nice.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •