Sailsjs/waterline 指定模型中的小数位数 [英] Sailsjs/waterline specify number of decimal places in model

查看:47
本文介绍了Sailsjs/waterline 指定模型中的小数位数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何告诉我的帆模型我想要一个 type: 'float' 属性的一些特定数字小数位?像 decimalPlaces: 4 或类似的东西?

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?

问题是,当我向该条目发布一个值时,磁盘上的值被截断到 .00(数百个)位置.假设我想要: 3243.2352362 原样存储.目前这被转换成3243.24

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

如果重要的话,我使用的是sails-mysql 适配器.

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

推荐答案

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

不过这是保留两位小数.我无法找到一种方法来动态更改 N,因为没有办法将参数传递给自定义验证.此问题的解决方法是检查 beforeValidation() 函数中的自定义小数位数.

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.

这篇关于Sailsjs/waterline 指定模型中的小数位数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆