Page 1 of 1
insert a number like 30,000 to the DB
Posted: Mon Dec 27, 2004 8:44 am
by pelegk2
how can i insert a value like 30,000 to a float cell?
beacuse currently it cuts it to 30!!!
thnaks in advance
peleg
Posted: Mon Dec 27, 2004 9:26 am
by feyd
remove the comma?
Posted: Mon Dec 27, 2004 9:38 am
by josh
Code: Select all
function uncomma($num) {
$num=str_replace(",", "", $num);
return($num);
}
function comma($num) {
$num=number_format($num, 0);
return($num);
}
// uncomma then put it into the datatabase, then comma it when you take it back out
Posted: Mon Dec 27, 2004 11:27 am
by Joe
You could also use round or printf to format your number.
Posted: Tue Dec 28, 2004 12:45 am
by pelegk2
thanks alot every one:)
ne more thing
Posted: Tue Dec 28, 2004 12:59 am
by pelegk2
how can i check that the number is a number and not a string?
thnaks in advance
peleg
Posted: Tue Dec 28, 2004 1:14 am
by crouse
Depends on what you mean by "how do I check that the number is a number and not a string."
If you mean that you want to make sure that a variable constains a number, regardless if it is a string or numeric type, then you would use the is_numeric function.
If you mean that you want to check to make sure that the variable is a numeric type and not a string type then you would want to use is_float or is_int depending on the type of variable that you are looking to evaluate.
The online documentation in the PHP manual, available on the PHP website, goes into more detail about each above mentioned function.
Chris
Posted: Tue Dec 28, 2004 1:23 am
by n00b Saibot
Good Good
Since when did u start coding Chris.
Posted: Tue Dec 28, 2004 1:36 am
by crouse
Thank you.
I have been programming professionally for about a year and a half. However, I have been off and on coding for about 6 years with a serious effort in the last 3 to 4 years.
Chris
i triedbut
Posted: Tue Dec 28, 2004 2:18 am
by pelegk2
for example i used : is_int ("30,000") and i got false

Another Suggestion
Posted: Tue Dec 28, 2004 3:16 am
by crouse
Unfortunately you are using the wrong function for what you are looking to do. What your code shows is that you are accepting a string type variable and the code is determining if it is an integer type.
I think what you are looking for at this point is the is_numeric function. The is_numeric function takes a variable and determines if the variable is either a number typed variable or if the string that you passed the function is a number. However, it is not as smart as some people might wish that it could be. The is_numeric function only works if all characters in the string are numbers themselves. So the is_numeric function breaks if you have any non-numeric characters in the string.
An example that you might help you out:
Code: Select all
<?php
if(is_numeric(str_replace(",", "", "30,000")))
print "Is a Number";
else
print "Is not a Number";
?>
This example takes the commas out of the string before evaluating the string to see if it is numeric.
Another example that you might be able to build on:
Code: Select all
<?php
if(is_numeric(str_replace(array(",", "$"), "", "\$30,000")))
print "Is a Number";
else
print "Is not a Number";
?>
This example does the same as the previous example however it also takes out dollar signs as well, just in case. There might be better ways to go about doing this however it is a start in the right direction.
Chris
Posted: Tue Dec 28, 2004 4:49 am
by pelegk2
ok thanks alot!!!:)