问题
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