Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

Data Model Questions /

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Float precision issue

Go to End

3 Posts   1821 Views


Community Member, 166 Posts

1 November 2011 at 6:37pm

Edited: 01/11/2011 6:38pm


I have a feeling that there is a bit of an issue regarding Float DBField types and the precision.

If I create a Float static, and setup a NumericField through which I enter values in the CMS, despite whatever depth of precision of value I enter, it always defaults to 3 decimal places.

As I set breakpoints through the code and step through, it appears that the correct value is being set initially (ie correct number of decimal places) but there is a problem (possibly with the underlying DB query) whereby only a 3 decimal place precision floating point value is read for display in the NumericField.

I can confirm this when I do an SQL query (or look at table via phpMyAdmin) on that field directly - it returns the value to 3 decimal places.

The reason I think this is a problem is that, even though the original setting of the value works, on successive reads and updates (even if the value in the NumericField in the CMS isn't altered) the value read value from the DB has less precision than the originally stored value and will be overwritten on the next "Save".

Any ideas on how to fix this or how to set the precision on Floats read into NumericFields? Would you agree that this is a bug?

eg. from Geocodable.php

	public function extraStatics() {
		return array('db' => array(
			'Lat'  => 'Float',
			'Lng'  => 'Float'

eg. CustomSiteConfig.php

       public function updateCMSFields(FieldSet $fields) {
                $fields->addFieldToTab("Root.Geo", new NumericField("Lat", "Latitude"));
                $fields->addFieldToTab("Root.Geo", new NumericField("Lng", "Longitude"));

Thanks very much.



Forum Moderator, 1899 Posts

1 November 2011 at 10:27pm


I didn't read through your whole post (sorry) - I just have the advice to use a decimal type instead to control the precisison...

		'Price' 				=> 'Decimal(6,2)',



Community Member, 166 Posts

3 November 2011 at 3:28pm

Hi Barry,

Thanks for your reply. Works well. Using Decimal(X,Y) caused an issue in dev/build, for which I posted on the forum. It's working well now.

Thanks again for your good advice.