Kinda Code
Home/Node/TypeORM: Entity with Decimal Data Type

TypeORM: Entity with Decimal Data Type

Last updated: May 20, 2022

In TypeORM, you can add a column with decimal data type like this:

@Column({type: "decimal", precision: 10, scale: 2, default: 0})
price: number;

Where:

  • precision: The number of digits in the decimal
  • scale: The number of digits to the right of the decimal point

Full example:

// Product entity
import { Entity, PrimaryGeneratedColumn, Column, CreateDateColumn } from 'typeorm'

@Entity()
export class Product {
    @PrimaryGeneratedColumn()
    id: number;

    @Column()
    name: string;

    @Column({type: "decimal", precision: 10, scale: 2, default: 0})
    price: number;
 
    @CreateDateColumn()
    createdAt: Date;
}

Further reading:

You can also check out our database topic page for the latest tutorials and examples.