tags:

views:

53

answers:

3

Hi guys,

what would be the best data type to store a floating point number in mySQL?

Can I somehow store it as an INT?

I am thinking that VARCHAR is my best option, but if you guys know of anything better I would appreciate the input.

Thanx in advance!

+5  A: 

I would use a Mysql Float type.

Steve Weet
+1  A: 

Why do you want to store a floating-point as an int? MySQL has decimal and float types just like your programming language.

I'll assume you have a good reason. To store a float as an int, you can try a few things:

  • Multiply the number by 10^n, where n is the number of significant digits you want to keep, and then truncate the rest of the fractional part. When you get it back out of the DB, convert to float/decimal and divide by n. This requires an int big enough to store the multiplied value; in 32-bit architecture, a "native" int can store values up to 2 billion.
  • Split the number into its integer part and its fractional part. This requires two fields, but each field can have a value up to the maximum integer value, allowing you to easily have precision in the hundred-millionths.
KeithS
A: 

If you have to encode a floating point number in a flat format I'd recommend having a look at http://en.wikipedia.org/wiki/IEEE_754-2008

PP