- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Should I store a field PRICE as an int or as a float in the database?
You do not need to store a field PRICE as an int or as float in the database. For this, you can set the DECIMAL()..
Most of the time integers can be used to represent the float point numbers and these integers are internally cast into DECIMAL() data type. Therefore, if you have field PRICE then always use DECIMAL() data type. The syntax is as follows −
DECIMAL(M,D);
Here, M represents the ‘TotalNumberOfDigit’ and D represents the ‘Number OfDigitAfterDecimalPoint’.
To understand the above concept, let us create a table with field PRICE as DECIMAL data type. The query is as follows −
mysql> create table Decimal_Demo -> ( -> Id int NOT NULL AUTO_INCREMENT, -> PRICE DECIMAL(18,2), -> PRIMARY KEY(Id) -> ); Query OK, 0 rows affected (1.63 sec)
Insert some records in the table using insert command. The query is as follows −
mysql> insert into Decimal_Demo(PRICE) values(99999999999999.99); Query OK, 1 row affected (0.11 sec) mysql> insert into Decimal_Demo(PRICE) values(10000000000000.99); Query OK, 1 row affected (0.36 sec)
Now you can display all records from the table using a select statement. The query is as follows −
mysql> select *from Decimal_Demo;
The following is the output −
+----+-------------------+ | Id | PRICE | +----+-------------------+ | 1 | 99999999999999.99 | | 2 | 10000000000000.99 | +----+-------------------+ 2 rows in set (0.00 sec)
- Related Articles
- What should I do? Select int as currency or convert int to currency format in MySql?
- How should I store data into my Mysql database, what type should I assign to a column storing salt values?
- How should I pass a matplotlib object through a function; as Axis, Axes or Figure?
- How can I store ‘0000-00-00’ as a date in MySQL?
- How to parse a string to float or int in python?
- How do I format a number as decimal to store it in MySQL?
- As an HR of a company, what kind of attitude should I carry at work?
- A dealer sells an article for Rs. 24 and gains as much percent as the cost price of the article. Find the cost price of the article.
- Ordering string as a number in a database?
- How can I store the fixed length string as well as variable length string in the same MySQL table?
- Performing a MySQL LIKE comparison on an INT field?
- Should I name the username field in my MySQL table “name” or “user_name”?
- Insert NULL value into database field with char(2) as type in MySQL?
- Can a Java array be declared as a static field, local variable or a method parameter?
- Make HTML text input field grow as I type in JavaScript?
