TypeORM: Entity with Decimal Data Type

Updated: May 20, 2022 By: Pennywise Post a comment

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.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

Related Articles