0

I have the following Angular 7 Typescript class:

export class Paging {

  itemCount: number;
  pageCount: number;
  pageNumber: number;
  pageSize: number;    

  constructor(pageNumber: number, pageSize: number, itemCount: number) {

    this.itemCount = itemCount;
    this.pageCount = itemCount > 0 ? (number)Math.ceil(itemCount / pageSize) : 0;
    this.pageNumber = pageNumber;
    this.pageSize = pageSize;

  }

}

I have 2 problems:
1. How to import Math functions (at the moment I am getting an error)?
2. How to convert the result of the calculation Math.ceil(itemCount / pageSize) to an integer?

2
  • 2
    Remove the (number) bit and you should be fine. Typescript doesn't have an integer type, only number, but Math.ceil will certainly return an integer in the mathematical sense Commented Nov 6, 2018 at 11:12
  • 1
    1)U no need to import math functions in typescript 2)try parseInt Commented Nov 6, 2018 at 11:26

1 Answer 1

1
  1. Like user184994 comment, just remove (number)

    this.pageCount = itemCount > 0 ? Math.ceil(itemCount / pageSize) : 0;
    
  2. Math.ceil returns integers: number (The smallest integer greater than or equal to its numeric argument).

If you need convert into numbers:

let str2deci = parseInt('123', 10);

let num = Number('456'); 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.