...

View Full Version : Formatting of Numbers - Need good reference material



CaseyC
12-26-2007, 04:08 PM
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........

oesxyl
12-26-2007, 07:22 PM
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:



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

CaseyC
12-26-2007, 07:55 PM
Thank you for the answer. I tried that, and came up with some funny results. Here is the code I inserted to test this:


$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']);

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

oesxyl
12-26-2007, 08:51 PM
Thank you for the answer. I tried that, and came up with some funny results. Here is the code I inserted to test this:


$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']);

$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, :thumbsup: Try to tell somebody now that you must deal with 256 bytes of RAM :)

best regards, happy holidays

Inigoesdr
12-26-2007, 09:28 PM
You don't need the mySQLsafe function if you're typecasting it to a float or int. You can also typecast like this:


$test = '12.34';
$test = (float) $test;


I was starting with fortran iv too, :thumbsup: Try to tell somebody now that you must deal with 256 bytes of RAM :)

Bytes? That would be nice. ;)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum