1

DataTypes.DOUBLE(10,2) or DataTypes.DECIMAL(10,2) returns integer if there is no decimal value after ".". Only returns decimal if there is any value other then zero.

Migration

'use strict';
module.exports = {
  async up(queryInterface, Sequelize) {
    await queryInterface.createTable('properties', {
      id: {
        allowNull: false,
        autoIncrement: true,
        primaryKey: true,
        type: Sequelize.INTEGER
      },
      serial: {
        type: Sequelize.INTEGER,
        allowNull: false,
      },
      name: {
        type: Sequelize.STRING,
      },
      type: {
        type: Sequelize.STRING,
        allowNull: false,
      },
      base_fare: {
        type: Sequelize.DOUBLE(10,2)
      },
      current_fare : {
        type: Sequelize.DOUBLE(10,2)
      },
      tenant_id: {
        type: Sequelize.INTEGER,
      },
      booking_id : {
        type: Sequelize.INTEGER
      },
      building_id : {
        type: Sequelize.INTEGER,
        allowNull: false,
        onDelete: 'CASCADE',
      }
    });
  },
  async down(queryInterface, Sequelize) {
    await queryInterface.dropTable('properties');
  }
};

Model

'use strict';
const {
  Model
} = require('sequelize');
module.exports = (sequelize, DataTypes) => {
  class Property extends Model {

    static associate(models) {
     // Removed for readability

    }
  }

  Property.init({
    serial: DataTypes.INTEGER,
    type: DataTypes.STRING,
    name: DataTypes.STRING,
    base_fare: DataTypes.DOUBLE(10,2),
    current_fare: DataTypes.DOUBLE(10,2),
    tenant_id: DataTypes.INTEGER,
    booking_id: DataTypes.INTEGER,
    building_id: DataTypes.INTEGER,
  }, {
    sequelize,
    modelName: 'Property',
    timestamps: false,
    omitNull: false,
    name: {
      singular: 'property',
      plural: 'properties'
    },
    underscored: true
  });
  return Property;
};

Sample Data

{
            "id": 11,
            "serial": 19,
            "type": "",
            "name": "Room",
            "base_fare": 2400,
            "current_fare": null,
            "tenant_id": null,
            "booking_id": null,
            "building_id": 1
},
{
            "id": 12,
            "serial": 19,
            "type": "",
            "name": "Room",
            "base_fare": 2400.5,
            "current_fare": null,
            "tenant_id": null,
            "booking_id": null,
            "building_id": 1
}

I want my "base_fare" to output 2400.00 ( fill the zeros ) if there is no zero or other digits. The numbers are automatically getting converted to integer if there is no non zero digits after decimal point. Is there any solution?

1 Answer 1

1

There is no fault of sequelize but javascript itself.
The only way to display as much zeros after delimeter as you want is to convert number into string.

The easiest way would be to use toFixed method.
In case of sequelize you either have to override getter of base_fare
or just map query result with:

let result = await ... //here is the model query  
result = result.map((x) => ({
    ...x,
    base_fare: x.base_fare.toFixed(2)
})
);

or

let result = await ... //here is the model query  
result.forEach( (x) => x.base_fare = x.base_fare.toFixed(2) );

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.