insert a number like 30,000 to the DB

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
User avatar
pelegk2
Forum Regular
Posts: 633
Joined: Thu Nov 27, 2003 5:02 am
Location: Israel - the best place to live in after heaven
Contact:

insert a number like 30,000 to the DB

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

remove the comma?
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post 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
User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

Post by Joe »

You could also use round or printf to format your number.
User avatar
pelegk2
Forum Regular
Posts: 633
Joined: Thu Nov 27, 2003 5:02 am
Location: Israel - the best place to live in after heaven
Contact:

Post by pelegk2 »

thanks alot every one:)
User avatar
pelegk2
Forum Regular
Posts: 633
Joined: Thu Nov 27, 2003 5:02 am
Location: Israel - the best place to live in after heaven
Contact:

ne more thing

Post by pelegk2 »

how can i check that the number is a number and not a string?
thnaks in advance
peleg
crouse
Forum Newbie
Posts: 13
Joined: Mon Dec 27, 2004 11:14 pm
Location: Sacramento, CA
Contact:

Post 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
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post by n00b Saibot »

Good Good :-)

Since when did u start coding Chris.
crouse
Forum Newbie
Posts: 13
Joined: Mon Dec 27, 2004 11:14 pm
Location: Sacramento, CA
Contact:

Post 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
User avatar
pelegk2
Forum Regular
Posts: 633
Joined: Thu Nov 27, 2003 5:02 am
Location: Israel - the best place to live in after heaven
Contact:

i triedbut

Post by pelegk2 »

for example i used : is_int ("30,000") and i got false :(
crouse
Forum Newbie
Posts: 13
Joined: Mon Dec 27, 2004 11:14 pm
Location: Sacramento, CA
Contact:

Another Suggestion

Post 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
User avatar
pelegk2
Forum Regular
Posts: 633
Joined: Thu Nov 27, 2003 5:02 am
Location: Israel - the best place to live in after heaven
Contact:

Post by pelegk2 »

ok thanks alot!!!:)
Post Reply