I want to use the jQuery spinner for a currency value (i.e. money), the value of which might be in dollars, euros, or anything supported by the spinner.
The data should then be saved into a mysql database, in one col I would save the currency, in another the value.
The problem is that I am not sure on how to do this. Is it better to save the data in the format provided by the spinner (therefore saving everything as varchar), or should I take the extra step of normalizing all the values in and reconverting according to the currency when out from the db?
Note that I would like to perform some calculations (simple algebra) on that data, but only with values of the same currency.
If you want to do some calculations with the inserted data, it's better to store it without , . / $.
And wherever you want to show the data, change it into currency format.
Related
I am writing an application that can upload values to specific fields in podio. When doing so i have no trouble uploading and receiving text fields, category fields, ect. But when uploading and receiving numbers, they return in the wrong format.
Example
Uploaded value: 50000
This will look like this in podio's formatting: 50.000
When retrieving the field it is formatted like this: 50000.0000
The field in podio is formatted normally with thousand separators as you can see.
Example 2
Some of the fields require the formatting to show 2 decimals.
So the uploaded value is 2.24
In podio this will be formatted like 2,24
Then when retrieving it returns 2.2400
Possible solutions:
It would be possible to manually format the fields, but the amount of zeros fluctuate. I wouldn't be able to know which zeros the user put.
A setting to change when retrieving the item?
When you upload a numerical value it is stored with 4 decimal places of precision. When that value is rendered in the web ui there is formatting applied. In the examples you provided you are not losing precision and are getting the expected results.
I am having an issue on choosing the right numeric data for my price as my country currency do not use floating point.
Example: in my country currency we do not use this- 12700.58-
example of our price: 127,000 (which is hundred twenty seven thousand) for us.
So which sql numeric data type..i should use?
Thanks
First of all FLOAT/DOUBLE are non-exact datatypes so you should avoid it. Better to use DECIMAL/NUMERIC because they are accurate datatypes.
In you example(only whole numbers) I would use simple INT to store price:
CREATE TABLE tab(price INT UNSIGNED);
INSERT INTO tab VALUES (120000), (10);
The value with thousand separator 127,000 is only presentation matter and it should be done in application layer. If you still need to format it in database use:
SELECT FORMAT(price,0) AS formatted_price
FROM tab;
SqlFiddleDemo
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 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
:)
Just wondering what the best way of storing currency values and symbols within a MySQL DB might be?
Thanks to browsing SO, I'm storing my amounts as a Decimal (19,2) value - which is fine - but am more concerned with the Currency symbol (e.g. €, £, $, etc.), as I want to allow the end user to set their own currency unit at the set up stage. I also want to avoid any uncertainty as regards, which are currently set at utf8 (both sides).
The way I have at the moment is to store them as HTML Numerical Codes using PHP ifelse statements to filter input. Is this the best method? If not, what is? Is there a need at all? Many thankee's in advance!
imho, you can break the data into two columns
amount = decimal(19,2) --- question : unsigned for positive value only
currency_id = int(10) unsigned --- which is ID to currency table
when the currency field is reference to another table,
you can storing all sort of additional info into that table (such as exchange rate)
to better describe how you want the symbol get presented
I would use a character (3) and store the currency code instead ( http://en.wikipedia.org/wiki/ISO_4217#Active_codes ). For example, EUR, USD, GBP etc., and only show the appropriate symbol at display-time.
Have you considered not using symbols at all, but using ISO 4217 three letter codes instead?
If necessary, use another table to handle mapping from those into a more user-friendly symbol format on output.
Do you have to convert the currency based on what the user choose? If yes, you can just make a new table for storing the currency symbol and the exchange rate.
If you just want to show the currency based on the user's locale, you may want to try an internationalization library.