How to correctly insert decimals to MySQL database - php

I would like to know the proper way to insert decimals to a MySQL database
When I use the type decimal and if I enter 0.20 to the database its saving the value as 0
And when I use the type float and if I enter 0.20 to the database its saving the value as 0.2
I want to display the same value that enter and also, I want to use this value for filters. So, I don’t want to use the varchar type to inset these values.
Can someone tell me what is the best way to do this? I wouldn't mind using PHP to display the missing end zero.
Appreciate your time.

10,0 means 10 characters 0 of which are decimal. 10,2 would mean 8 numbers to the left, 2 to the right of the decimal.
Float should only be used when you are dealing with extremely large or small numbers; like the number of atoms in the galaxy where precision does not matter but scale does.
Lastly don't mix the concepts of "Display"(format) and value. Display is a formatting matter. The value 0005.50000 is the same as 5.5. Their display is just a matter of formating.

Related

How can store exact decimal value in database

1 * 20/100 = 0.2
I want to store the same value in database how can i do that
I have give column type as Decimal and the lenght automatically taking as (10,0)
My issue is it is storing as 0 instead of 0.2
No datatype will store 1/3 exactly.
Your example of 20/100 implies the need for DECIMAL(..., 2), where the ... is a suitable maxinum number of digits including the 2.
Without further insight into where the numbers are coming from or how they will be used, we cannot discuss this further.
Change the limit of that field as (10,2) and then try again. It might help you.
Use type "double" (10,2) in your database.
double is type and length is 10,2
it will store like 0.20
if your want to store only single decimal digit the use 10,1
Thanks.

How to correctly send a decimal to a mysql database?

I am trying to get MySQL decimal field types to work nice and I just can't figure it out. I send a $_POST['price'] field to a decimal field, for example 3.45, and it always stores it as 3.00.
What would I be doing wrong for it to not store the decimals?
Found the solution: PDO::PARAM for type decimal?
PDO has not decimal storing type, seems like something massive to miss out. Need to store it as a string not a numeric number.
Use floatval() when converting the $_POST['price'] variable from a string to a floating point. This value is what should be stored in your database.
Be wary though! Floating point arithmetic is not so great when computing prices due to possible rounding errors. When dealing with price, I typically use integer arithmetic and then manually put in the decimal point.
So In your example I would store 345 to represent $3.45

mySQL: I need to input a decimal into an int column

I have a table with a current structure as follows:
Currently this is populated as follows:
The data stored for product value is a decimal value
and the end digits are cut off once it is inserted into the database.
I have tried changing the table structure as follows:
However this only leads to the following:
As you can see all values have a .00 appended if none exists, however I want to
store all these values with no decimal places. Except the product value.
How can I do this?
The trouble is you are converting a decimal (float / double) to an integer, so the value is simply truncated (decimal values are chopped off).
If you really don't want to use floats (decimal values) in the database you can use this hack work around will work:
Multiply the number by 100 before inserting it, and then be sure to divide it by 100 when you use the data. This will allow you to maintain 2 decimal points while using integer storage.
Thus, 2.4 would be stored as 240, 53 would be 5300, 20.74 becomes 2074 etc...
I want to note that this is not an ideal solution, but rather a hack.
I highly recommend what the other users suggested in the comments: storing the decimal value (as you have) and formatting it when presenting it.
--- In addition ---
Your real problem appears to be with the way the database is setup.
Each of those values should have their own field since they will be repeated for each product.

Why are the decimals not adding up correctly?

I'm working on a program where you can choose up to 3 things you want to divvy points amongst.
Say for example that an action gains you 4 points, and those 4 points are divvied amongst the 3 things you selected.
In this case, those 3 things each get 1.33333... points.
In my database, they are stored as 1.33.
However when I bring them out, it tallies up to 3.99.
Understandable.
But how can I avoid this without giving one of the things 1.34 points?
Store the full float/double in your database rather than truncating to 2 decimal places. The time to trunc is when displaying the value to the user -- but only trunc the displayed string, not the actual value.
Floating point values are the annoying drunk uncle of computing. Just let them be what they are, and then clean them up when presenting to the public eye.
Floating point numbers will be lossy in this case. If you are dealing with integer numerators and denominators, why not store the numbers as fractions? You can make use of Pear's Math Fraction library or write something yourself.
Use a third decimal place - not for display, but only for tracking precision. If someone divides 4 points among three, store it as 1.333. When you calculate back, you get 3.999 which you round up to 4. On the other hand, if someone divides 3.99 among three objects, store it as 1.33, so when you calculate back, you get 3.99 (and not 3.999) and thus you know not to round up.

MySQL greater than with microtime timestamp

I have one PHP script inserting rows in a MySQL database. Each row has a field 'created_at' which is filled with the value of the PHP function microtime(true), and inserted as a double. (microtime because I need something more precise than to the second)
I have another PHP script that selects rows based on that created_at field.
When I go ahead and select like this:
SELECT * FROM `ms_voltage` WHERE created_at > 1302775523.51878
I receive a resultset with, as the first row, the row with exactly that value for created_at.
This occurs from within my PHP script and from within PhpMyAdmin when manually doing the query. But not always, not for every value. Just once and a while really.
How is this possible? I didn't ask for greater than/equals, I want strictly greater than.
Am I overlooking something type-related perhaps?
Yeah, floating point arithmetic can do that sometimes. To understand why, it's helpful to realize that just as not all numbers can be accurately represented in base 10, not all numbers can be accurately represented in base 2 either.
For example, "1/3" may be written in base 10 as 0.33333 or 0.33334. Neither is really "correct"; they're just the best we can do. A "DOUBLE" in base 10 might be 0.3333333333 or 0.3333333334, which is double the digits, yet still not "correct".
The best options are to either use a DECIMAL value, or use an INT value (and multiply your actual values by, say, 10000 or 100000 in order to get the decimal digits you care about into that int).
The DOUBLE type represent only approximate numeric data values. Try to use the DECIMAL type.
Is your column floating point? Calling microtime with true gives you a float, and that looks like a float, which will have digits after the .51878 that you don't see, so those digits make the stored value greater than the value you have in your query.
Unless you really need the float I'd convert the string result to an int, or even two columns for seconds and useconds. Then you can use > or < on known values without worrying about the imprecision of the floating point value.

Categories