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
Related
I'm running into a problem here. I'm storing prices in my database as a string in the following format: 14.500,00 and 199,95. Sometime later I created this range slider so the users can filter on price as you can see in the provided image. For this to work, I needed to write a new query so I was thinking of a BETWEEN in SQL but this doesn't work on strings. Any ideas to filter on price with a range slider in SQL?
BETWEEN does work on strings. It works just fine -- with the strings ordered alphabetically.
Your problem is that BETWEEN on strings doesn't follow the numeric ordering. Well, that is normal. If I'm speaking French, I wouldn't expect an English speaker to understand me. The same with types. If I use BETWEEN on strings, then I expect the comparisons to be string-based, not numeric. (The same is true of dates, by the way.)
Fix your data so the values are stored as numeric/decimal values. These are numbers with a fixed number of decimal places, exactly what is needed for monetary values.
In most databases, you will need to get rid of the dollar sign. Something like this should work:
update t
set price = replace(price, '$', '');
alter table t alter column price numeric(10, 2); -- or whatever is appropriate
The exact syntax might vary, depending on the database.
I have single record on a table. So on MySQL when
select myamount from table 1 -- returns amount 420.67
But when i do MySQL as
select sum(myamount) from table 1 -- returns amount 420.8699951171875
should n't it return same amount 420.67 since I have only one record? and how to get amount 420.67 if SUM used.
Any help is appreciated and yes myamount datatype is float.
Float variables are stored in "scientific notation" (the 2,4E+04 format, which is the same as 2,4*10^4). But to make it even worse, it is also stored in binary. When calculating things with numbers stored as float, you may get a bit strange results because of this.
This video by Computerphile describes the problem very nicely.
I have code that searches for cars depending on your price range:
$category = "SELECT * FROM tbl_listings WHERE price between '$c[0]' AND '$c[1]'";
For some reason, that code doesn't work perfectly. It showed me a couple cars in the right range, but also showed one that is 200,000 when I was searching between 5,000 and 20,000.
Also, what is a good way to search when some cars have a price with a dollar sign in the database and some have commas? The search form is not returning anything with a dollar sign or commas.
Stop storing prices as strings? A price is typically stored as one of two types:
integer: number of cents
float: dollars and cents, but be sure to set the number of decimal places to 2
One doesn't generally store prices as strings (like "$14,999.99") in the database because you can't do range queries, like the one you're trying to do now.
You also can't do arithmetic, like a query that totals the prices of a particular subset of cars.
If the data you're pulling in has formatted strings like this, use NumberFormatter::parseCurrency() to get a float from the string you're given before shoving it in the DB. http://php.net/manual/en/numberformatter.parsecurrency.php
Your statement about
some cars have a price with a dollar sign in the database and some
have commas
makes me think the datatype in the database are not numeric datatype. This can be an issue, even provided that your $c[0] is correctly the lower bound and $c[1] is the upper bound.
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.
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.