Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to store 1/3 as a decimal in MySQL

I am trying to figure out how to store 1/3, or any fraction which results in an infinitely repeating decimal value in MySQL. I cannot just use 3.333333 because it obviously does not total to 100. I have been reading about the float datatype but i'm not sure if this will work. Any help would be appreciated.

Thank you

like image 664
Progger Avatar asked Dec 02 '22 23:12

Progger


1 Answers

You could potentially represent all rational numbers (including integers) as "numerator" and "denominator". So in your table you'd have a numerator and denominator columns, and your app would have logic to store numbers using that form.

You would still be unable to store irrational numbers precisely with this technique (i.e. if you want to store Pi, you'd need a fractional approximation anyway).

See here for what rational numbers are, so you can understand the limitations of this technique.

http://en.wikipedia.org/wiki/Rational_number

like image 116
Roadmaster Avatar answered Dec 29 '22 06:12

Roadmaster