I'm creating a decimal field to hold a financial figure in Doctrine2 for my Symfony2 application.
Currently, it looks like this:
/**
* @ORM\Column(type="decimal")
*/
protected $rate;
When I entered a value and said value was persisted to the database, it was rounded to an integer. I'm guessing that I need to set the precision and scale types for the field, but I need someone to explain exactly what they do?
The Doctrine2 documentation says:
precision: The precision for a decimal (exact numeric) column (Applies only for decimal column)
scale: The scale for a decimal (exact numeric) column (Applies only for decimal column)
But that doesn't tell me an awful lot.
I'm guessing precision is the number of decimal places to round to, so I assume that should be 2, but what is scale? Is scale the significant figures?
Should my field declaration be this? :-
/**
* @ORM\Column(type="decimal", precision=2, scale=4)
*/
protected $rate;
Just a quick note: I had to remove the quotes from the attributes precision and scale, like so:
Doctrine uses types similar to the SQL types. Decimal happens to be a fixed precision type (unlike floats).
Taken from the MySQL documentation: