I just want to ask if there is any PHP/MySQL datatype that can store a number with a comma and decimal such as 10,000.35
when the user hit the save button with this value, it should be stored in a MySQL table and the system can retrieve it also to be processed as number 10000.35
thanks for any help!
That would be a CHAR/string datatype.
Numeric values don't have formats. They only contain the numeric value. Commas are not relevant for numeric computation. Format the values on output using, for instance, number_format. That's the only time a comma is relevant, it does not need to be stored.
Store the number without the comma in MySQL and just format the number in PHP when you are displaying it. It would be easier to keep the number without the comma in PHP as well if you're doing math with it - only use the comma when displaying the data!
If you are going to be storing numeric values it is best to leave all the formatting out of it.
For instance, what if you need to localize the display so that 10,000.05 needs to be 10.000,05? You'd have a lot of work to do.
You should store the value in the database as 10000.05 and use number_format($myValue,2,'.',','); to display the value when it's time. This will allow you to change the literals to variables or constants should you ever have to localize. It will also allow you to configure how many decimal places you care to display.
Here's the PHP docs for number_format()
It would be best to store it in your MySQL database without the comma, and then using PHP's number_format to display it with the commas.
The MySQL datatype that does this is DECIMAL. DECIMAL gives you fixed decimal places without the precision errors inherent in float type. (ie 123.45 instead of 123.4499999999999999)
You have 2 options , using MySQL to format directly or Using PHP .... see below for examples
MySQL Direct Solution
SELECT FORMAT(number, 2) as formatNumber FROM table ;
PHP Solution
number_format($number,2,'.',',');
Thanks
:)
Related
In my app i want save float or double values that are calculated using php into mysql.
I also want to make calculations with these columns in stored procedures using sql.
But i notice that double and float data types show different in php and mysql.
for example after a calculation in php i might get a value 4.00 while the same calculation is mysql is 4.0027.
How can i make possible to have more or less the same value calclated. what type should my columns in mysql be and what cast should i make in php?
Thanx
You may read this page for answers why sum of two integers often results in a float.
Considering the MySQL data type, the difference is discussed and explained in this question: Difference between float and decimal data type
I would suggest to store in decimal with some rounding before insert/update in PHP.
Again, everything depends on the calculations you perform on the values.
Try this
use number_format("4.0024",2); output 4.00
or Update
use number_format("6556564.0024",2, '.', ''); or number_format(6556564.0024,2, '.', ''); output 6556564.00
as usual, did my duty to look everywhere for the sol but to no avail.
mysqli_fetch_assoc is (apparently) storing my numbers as strings.
Normally, I could care less, but my site is nearly 100% ajax, and it moves around a lot of data, so all of those json "'s start to add up.
If I'm just grabbing one column value, I can intval, but I want to grab whole rows with associated column names.
Is there a way to get mysqli_fetch_assoc (and PHP PDO's fetch assoc) to store numbers as numbers?
Many thanks in advance!
MySQL can store 64-bit numbers and unsigned numbers, neither of which are supported by default in PHP's int type. If you try to fetch numeric data from the database that overflows the int variables in PHP, you'll lose information. That's why PHP database clients fetch numbers as strings.
Re your comment:
Yes, if you're concerned about the size of JSON, convert the data in your PHP app for more compact representation.
Though since you said you're transferring column names as part of your JSON, it seems like the difference between string-formatted ints and binary ints is not your bottleneck anyway.
I mean, converting a string-int to a binary int reduces the bits by about 50%. But if you have column names as strings anyway, you're only shrinking a small portion of the JSON. The columns are still strings.
I am in need of storing a score in a mysql field but this score could be a time,integer or float. My first way of doing this was to identify the score type and then enter it into one of three fields but if the need arises to add a new score type I dont want to have to continually add a field to the database. I remember somewhere down the line someone told me that if you store somethign as a varchar then is wont be able to be read as an integer or float or even date. My question is, can I store all three of those as one specific type but read it any way I need when taking it from the database and using it in my php code?
In my opinion you could model the field as FLOAT except if you absolutely need to know about the type of variable stored. Time can be converted to an integer value by converting to timestamp. Integers are a subset of the real (floating point) numbers set actually so I guess that way you have everything covered. Floating point arithmetic can cause some issues with precision and equality testing though so be careful!
You can use CAST and CONVERT functions to convert the string datatype into another MySQL datatype such as INT, FLOAT, DECIMAL, DATE, DATETIME etc.
There are a few issues. How do you know what datatype is stored in a row? MySQL does have RegExp support but I do not recommend using it in your WHERE clauses. Store the datatype in another column.
Also, using cast functions in the where clause of your query will make them run slow. If you need to search upon/sort by the data you should use proper datatypes. Perhaps you should add one column for each datatype; and for each row, populate only the corresponding column.
mysql will happily convert text to numbers of the appropriate type if you perform a mathematical operation on it. However, it will also convert non-numeric text to zero and perform the same operation on it, so you need to check that you're only pulling fields of the appropriate type beforehand.
This forum post shows how to add a regular expression condition to your query to ensure that you're only pulling fields with numeric data in them. However, I think it's probably wiser to use a separate column to indicate what type of score each record is, and use that to retrieve the appropriate ones.
I don't know how to convert text to a date (putting it through date() doesn't work). However, note that the mysql date format (2012-05-08 11:20:23) has the date elements in order of descending significance. If you just want to find the highest / lowest date, or sort by date, treating them as strings will work fine.
I have a form in which users can enter prices for items. Ideally I want the user to be able to add prices in whatever method feels best to them and also for readability. I then need to convert this to a standard float so that my web service can calculate costs etc.
The part I'm struggling with is how to take the initial sting/float/int of currency and convert it into a float.
For example:
UK: 1,234.00
FRA: 1 234,00
RANDOM: 1234
RANDOM2: 1234.00
All of those have slightly different formats.
Which I would want to store as:
1234.00
I will then store the result in MySQL database as a DECIMAL.
Any help would be great.
Assuming you're using MySQL, use the DECIMAL or NUMERIC type are the correct types used for storing currency.
Float's are susceptible to rounding errors and have a limited precision.
The formatting for display should be handled by PHP.
If storing in DB, you should of course store a currency code - which can be used when retrieving to tell PHP how to display it
Couldn't you use:
floatval($AnyVar)
In a case where you'd like to accept so many different formats it's a bit tricky to get it right.
Now we can just use a simple regex to get the decimal and full parts of the value:
/^([0-9,. ]+?)(?:[.,](\d{1,2})$|$)/
The regex will capture the full part of the number + a decimal part, separated with a , or a . and which has one or two numbers.
The capture group 1 will contain the full part, and group 2 the decimal part (if any).
To get your number, you just need to filter out all non-numeric characters from the full part, and join the filtered full and decimal parts together.
If you want to make it more foolproof, you probably should implement something on the client-side to guide the user to input the value in the correct format.
How do i accept values from user that contain multiple points like 1.2.1...if i use float--1.2.1 gets converted to 1.2.
Thanks.
the simple answer: if u want multiple points DONT USE FLOAT :-)
use something like varchar or text instead
Treat it as text.
A float is used to represent Real numbers, and "1.2.1" is not a Real number.
Or, if "1.2.1" is simply a grouping of numbers, you could split the input of "1.2.1" into three separate numbers using the period as a delimiter, and store them as distinct numbers.