![](/img/trans.png)
[英]How to limit set the maximum decimal places of a number as 8 decimal places in React using javascript
[英]Is there a way to limit the number of decimal places returned using sequelize select query?
我只是想知道从我的数据库中的视图中选择时,是否有办法限制网页上显示的小数位数。 数据类型是双精度,小数位数最多为 17。我想知道在查询时是否有办法将小数位数限制为 2 或 3。
这是我的 controller
const chw = await Prjt_source_percent_each.findOne({
where: {
project_id,
commodity: 'CHW',
phase: 'predicted'
}
});
这是我的 model。 我对底部的 Savings_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
。
您可以要求将基础二进制值格式化为 output,使用您请求的尽可能多的小数位。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.