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.
Related
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.
i've try using varchar, the problems below :
if the points 1000, then minus 10000, it'll not change the value to -9000.
then try again using int, the problems :
points can't have any , (comma) like 10000.21 it'll change the value to 10000
any ideas of this?
Use DECIMAL datatype if you want to use decimals :)
EDIT:
Standard SQL requires that DECIMAL(6,2) be able to store any value with five digits and two decimals, so values that can be stored in the salary column range from -9999.99 to 9999.99
Just use DECIMAL data type.
INT can't be decimal.
EDIT: Yeah, DECIMAL, not DOUBLE.
I have field where i store my grand total to a mysql filed as total and its type set as decimal(10,2)
When my php form adds up and updates the query for 112381348.40 (i.e 112 million)
but when i fetch or view it on phpMyadmin, the value shows as 99999999.99
from my data type set, it has to be 10 digits and 2 decimals where as it is maximum updating as 8 and 2.
Am i understanding wrong or what am i doing wrong.
I know i can increase it to decimal(13,2) and then see it.
But i would like to know the reason, why 10,2 is not happening?
The specification for decimal(m, n) takes two arguments. The first is the "precision" and the second is the "scale". More colloquially, the first is the number of digits in the number. The second is the number of digits to the right of the decimal point.
So, your specification of decimal(10, 2) can store numbers like: 12,345,678.90 -- i.e., up to 99,999,999.99. And that is the maximum number.
If you want to store numbers larger than that, then use a larger precision for the number. For your number, you need at least decimal(11, 2), although I would suggest a larger precision so you don't encounter this problem in the future.
In setting a float or decimal field, the first int value is the total character count. The second is how many decimal places. So:
//decimal(10,2)
12345678.90
//decimal(13,2)
12345678901.23
//decimal(7,3)
1234.567
See?
You have 11 digits. The first number is the total digits. Try:
DECIMAL(11, 2)
Data type in php (that I should set for a column in phpmyadmin) for entering both character and decimal numbers. Ex. 0.09 or n.a. or 100 (1) all such things can be feed in to the column.
A VARCHAR type can be used. This will store a string allowing your characters or decimals. Be sure to set an appropriate length - not too short or the data will be truncated.
If you find yourself storing lots of different types of data in a single field, then it may suggest you could use a better table/db design.
I would personally store that data in two different fields:
one INT for whole numbers
and one DEC(m,n) for decimal numbers * m is number of all digits and n is number of digits behind the decimal separator.
For example: DEC(5,2) data for this would be 123.45
If you need it to be in one field use VARCHAR but this is not my recommend, since it is not good to store integers in varchar type field.
Though a varchar is not recommended, but if you are using a varchar make sure you typecast the variable in your code while fetching.
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.