Jump to:

3460 Posts in 1064 Topics by 739 members

Data Model Questions

SilverStripe Forums » Data Model Questions » Float precision issue

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

Page: 1
Go to End
Author Topic: 818 Views
  • vwd
    Community Member
    158 Posts

    Float precision issue Link to this post


    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.


  • swaiba
    Forum Moderator
    1792 Posts

    Re: Float precision issue Link to this post


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


  • vwd
    Community Member
    158 Posts

    Re: Float precision issue Link to this post

    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.

Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.