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 /

Float precision issue


Reply


3 Posts   963 Views

Avatar
vwd

Community Member, 160 Posts

1 November 2011 at 6:37pm

Edited: 01/11/2011 6:38pm

Hi,

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. [url=https://github.com/ajshort/silverstripe-addressable/blob/master/code/Geocodable.php]from Geocodable.php[/url]

   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.

VWD

Avatar
swaiba

Forum Moderator, 1808 Posts

1 November 2011 at 10:27pm

Hi,

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)',

Barry

Avatar
vwd

Community Member, 160 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 [url=http://www.silverstripe.org/data-model-questions/show/18458#post309306]posted[/url] on the forum. It's working well now.

Thanks again for your good advice.
VWD.