Jump to content

Recommended Posts

Posted

Hello fellas...I need some clarification on something.

 

Ok...i'm not clear on mysql float datatypes. I mean, my script is working but I would like to know exactly what its doing.

 

I'm running a particular query...

>$sql = "UPDATE table SET votes=votes+1, votecount=votecount+$rating, rating=votecount/votes WHERE linkid=$id";

mysql datatypes are as follows

votes (int)

votecount (int)

rating (float)

 

the part i'm unclear about is the division i used here "rating=votecount/votes"

I read up a little at mysql.com and it discusses some arithmitic operations, but not division... so i'm dividing 2 integers and I want to know what type of division mysql does (percision wise).

 

This is what i'm getting pushed into the database..Ex. (29/6) = 4.83333

 

You would think since its a repeating decimal, it would push the maximum length of numbers, but that isn't the case.

Posted

Hi section31,

 

I haven't done a lot of MySQL programming, but I've been programming object oriented languages such as C++ and Java for several years. In those languages, when I came upon a dilemma such as this, I would simply cast the resultant value into whatever data type form I wanted.

 

For instance, consider the following line of code:

>int x;
int y =10;
int z = 3;

// If left uncast, the following line would give some sort of error because y/z is not an integer. The actual value is a never-ending decimal 3.333......
x = y/z;

// However, if I cast the result as an integer (to match the data type of "x"), the line will execute smoothly, and give the result of x=3
x = (int)y/z;  // Casts 10/3 as an integer before assigning the value to variable x.

 

I don't know if casting is an option in MySQL, but it was worth a mention!

 

Good luck!

:)

Posted
If you're concerned about the decimal, you can use the MySQL ROUND(). Check out the Mathematical functions.

No i wanted a decimal...just unsure why it rounds to the 6th decimal place...

 

Oh...another quick question if anyone can help..anyone know which is faster...mysql LOCATE function or LIKE.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...