What Type is best for long decimal number in SQL database?

Place for questions and answers for all newcomers and new coders. This is a free for all forum, no question is too stupid and to noob.
Post Reply
User avatar
Verahta
Posts: 440
Joined: Wed Aug 24, 2011 1:50 am

What Type is best for long decimal number in SQL database?

Post by Verahta »

I need to have numbers with values like this in my database (using SQL/MySQL/PHPmyAdmin): 4785.556448294

For type (int/float/decimal/double), which one is the best for this kind of number value?

Thank you ol wise ones!!! :D
"In order to understand recursion, one must first understand recursion".
User avatar
Nexus
Posts: 293
Joined: Sat Jun 18, 2011 10:42 pm

Re: What Type is best for long decimal number in SQL databas

Post by Nexus »

User avatar
Verahta
Posts: 440
Joined: Wed Aug 24, 2011 1:50 am

Re: What Type is best for long decimal number in SQL databas

Post by Verahta »

http://dev.mysql.com/doc/refman/5.5/en/ ... types.html

Turns out the Float Type wont work for me, it does approximate values and rounds. I need to store exact, set numbers. But, the numbers like the one I posted in my OP, is created by combining two difference values (one before and after the decimal). Now, it occurred to me, that the number on the right will always be unique, but the number on the left can be repeated many times by other number combinations.

So like this: 4785.556448294

4785 = a value on left side of decimal that could be repeated in combination with many unique values on right side of the decimal.

. = the decimal that both separates AND combines the number into a unique total value.

556448294 = a value that is unique to a single database entity only. But which could have several different values on left side of decimal. Like 1275.556448294 or 3496.556448294, and then each of 1275 and 3496 could some time be on left side of many unique future numbers/entities in the DB.

So now I'm thinking instead what I should do, is have two different tables, with primary keys for each set of numbers, then a third table to combine/match numbers and create the entire number sequence (like with repeating number's id in one column, and unique numbers id in other column)... then how to combine it? I guess I need to figure out if I want the numbers to exist only separately in the database and then only be "combined/made whole" when PHP echos to the page, or do I want to have a 3rd/4th table that stores the combos in entirety as another entity?
"In order to understand recursion, one must first understand recursion".
User avatar
fang
Posts: 31
Joined: Sat Feb 04, 2012 3:17 am

Re: What Type is best for long decimal number in SQL databas

Post by fang »

Or you could just store exact values in strings (VARCHAR perhaps) and simplify the problem (convert application-side).
Python/PHP/Javascipt . HTML/CSS . MySQL/CouchDB . Apache/Nodejs . git/sh/ssh . Windows/MacOSX/Linux
User avatar
Callan S.
Posts: 2042
Joined: Sun Jan 24, 2010 5:43 am

Re: What Type is best for long decimal number in SQL databas

Post by Callan S. »

if you know how many digits long the number is, you could multiply it to make a really, really large number and store that. Then when you get it back, divide it to make it back to a fraction

ie
4785.556448294

becomes

4785556448294
User avatar
Verahta
Posts: 440
Joined: Wed Aug 24, 2011 1:50 am

Re: What Type is best for long decimal number in SQL databas

Post by Verahta »

Okay I will try both ways, thanks for the suggestions guys.
"In order to understand recursion, one must first understand recursion".
Post Reply

Return to “Beginner Help and Support”