Sailsjs/waterline specify number of decimal places in model

假装没事ソ 提交于 2019-12-21 19:54:49

问题


How do I tell my sails model that I want some specific number decimal places for a type: 'float' attribute? Like decimalPlaces: 4 or something of that ilk?

The problem is that when i post a value to this entry, the value on disk is truncated to the .00 (hundreds) place. Say I want: 3243.2352362 to be stored just as it is. Currently this is transformed into 3243.24

If it matters I'm using the sails-mysql adapter.


回答1:


  types: {
    decimal2: function(number){
      return ((number *100)%1 === 0);
    }
  },
  attributes: {
    myNumber: {
      type: 'float',
      decimal2: true
    }
  }

This is for 2 decimal places though. I cant find a way to make it for dynamically changing N as there is afaik no way to pass a parameter to custom validation. Workaround for this issue would be to check for custom amount of decimal places in beforeValidation() function.




回答2:


I would not recommend (and I don't think its possible for float, maybe) adding a constraint in the model.

I'd suggest that you set:

migrate: "safe"

in your model and set the appropriate datatype/decimal Places in your tables.



来源:https://stackoverflow.com/questions/26617386/sailsjs-waterline-specify-number-of-decimal-places-in-model

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!