[英]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.