我只是想知道是否有办法限制从我的数据库 View 中选择时网页上显示的小数位数。数据类型为 double ,小数位数最多为 17。我想知道是否有办法在查询时将小数位数限制为 2 或 3。
这是我的 Controller
const chw = await Prjt_source_percent_each.findOne({
where: {
project_id,
commodity: 'CHW',
phase: 'predicted'
}
});
这是我的模型。我对底部的 Saving_percent 感兴趣。'use strict';
const {
Model
} = require('sequelize');
module.exports = (sequelize, DataTypes) => {
class Prjt_source_percent_each extends Model {
/**
* Helper method for defining associations.
* This method is not a part of Sequelize lifecycle.
* The `models/index` file will call this method automatically.
*/
static associate(models) {
// define association here
}
};
Prjt_source_percent_each.init({
project_id: DataTypes.STRING,
phase: DataTypes.STRING,
commodity: DataTypes.STRING,
comm_type: DataTypes.STRING,
source_energy_baseline: DataTypes.REAL,
source_energy_savings: DataTypes.REAL,
savings_percent: DataTypes.DOUBLE
}, {
sequelize,
modelName: 'Prjt_source_percent_each',
tableName: 'prjt_source_percent_each',
timestamps: false
});
Prjt_source_percent_each.removeAttribute('id');
return Prjt_source_percent_each;
};
最佳答案
一个浮点字段包含一个 精确的 数字量——也就是说,它的浮点二进制表示。它们变成了“可打印的东西”,以便您可以看到它们——但是当您实际查看 1 和 0 时,打印为 2.32
的东西可能是也可能不是那个数字。出于这个原因,在查询中处理浮点数时(在任何上下文中......),您通常需要使用小范围。 X >= 2.32 AND X <= 2.32001
。
您可以要求使用尽可能多的小数位来格式化基础二进制值以进行输出。
关于javascript - 有没有办法限制使用 sequelize select 查询返回的小数位数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67905233/