Pages

Saturday, February 22, 2014

Decimal, Float or Integer in Drupal Fields?

Drupal comes with 13 fields and several of them deal with numbers.
Newcomers to Drupal can become confused about the difference because there are 5 different number fields available:
  1. Decimal
  2. Float
  3. Integer
  4. List (float)
  5. List (integer)
In this tutorial, we're going to explain the difference between those 5 number fields and explain when you would use each one.

Integer

Integer is the easiest table of number field to understand. Integer is another word for whole numbers.
Integers are whole numbers such as 1, 2, 3, 10 or 500. This is ideal for numbers such as ages or years.
There are no settings at all for Integer fields:
media_1387475209862.png
You can't enter any spaces, letters or any punctuation into Integer fields.
Things such as spaces, commas or decimal points are added from the Manage Display tab:
media_1387475385532.png

Decimal

The Decimal field allows numbers with fractions.
You can decide on three settings for the field, including:
  • Precision: the total number of digits, both before and after the decimal point.
  • Scale: the number of digits after the decimal point.
So in the example below, a Scale of 2 would be ideal for storing prices. This is because there can only be two numbers to the right of the decimal point in prices such as $32.89
media_1387475296029.png
As with all of the number fields, you can't enter any spaces, letters or any punctuation into Integer fields.
Things such as spaces, commas or decimal points are added from the Manage Display tab:
media_1387475358659.png

Float

Float is definitely the most confusing of the umber fields.
Here's our simple rule: if you don't know what a floating point number is, you don't need to use the Float field.
However, for those of you that don't know but are curious, here's a quick explanation:
"Floating point" is a term used in computer programming. Basically, floating point numbers are numbers that contain floating decimal points.
Here's the best, simple explanation I've read about why floating points are used: http://floating-point-gui.de/formats/fp/
"Since computer memory is limited, you cannot store numbers with infinite precision, no matter whether you use binary fractions or decimal ones: at some point you have to cut off. But how much accuracy is needed? And where is it needed? How many integer digits and how many fraction digits?
    • To an engineer building a highway, it does not matter whether it’s 10 meters or 10.0001 meters wide - his measurements are probably not that accurate in the first place.
    • To someone designing a microchip, 0.0001 meters (a tenth of a millimeter) is a huge difference - But he’ll never have to deal with a distance larger than 0.1 meters.
    • A physicist needs to use the speed of light (about 300000000) and Newton’s gravitational constant (about 0.0000000000667) together in the same calculation.
To satisfy the engineer and the chip designer, a number format has to provide accuracy for numbers at very different magnitudes. However, only relative accuracy is needed. To satisfy the physicist, it must be possible to do calculations that involve numbers with different magnitudes.
Basically, having a fixed number of integer and fractional digits is not useful - and the solution is a format with a floating point."
Here are the field settings for a Float in Drupal. The only thing you can choose is whether to use a decimal point or a comma.
media_1387475251888.png
The Manage Display settings are the same as for Decimal.
media_1387475326005.png

List (float) and List (integer)

Both of these fields allow you to create a list of pre-defined choices. Unlike the other 3 fields, people will not be allowed to enter their own numbers.
media_1387477816235.png
If you choose "Check boxes/radio buttons", then this is how the options will show. If you want to remove the N/A option, set the field to be required.
media_1387477879075.png
If you choose "Select list", then this is how the options will show:
media_1387477972391.png

No comments:

Post a Comment

Thanks for your comment.