Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert number to string inside angular 2 component:

Inside the component i have

export class AppComponent implements OnInit {


public errorMsg :number =10 ;

public doughnutChartOptions: any = {
 cutoutPercentage: 85,
  elements: {
    center: {
      text: this.errorMsg + ' Energy produced in the Energy',
      fontColor: '#fff',
      fontFamily: "'Helvetica Neue', 'Helvetica', 'Arial', sans-serif",
      fontSize: 24,
      fontStyle: 'normal'
    }
  }
};

}

  text: this.errorMsg + ' some thing',

This is a property accepted string , so here i have to pass the number to string , How to do that in angular4,typescript 2.5

like image 525
Mohamed Sahir Avatar asked Nov 07 '17 11:11

Mohamed Sahir


People also ask

How do I convert a number to a string in TypeScript?

TypeScript - Number toString() This method returns a string representing the specified object. The toString() method parses its first argument, and attempts to return a string representation in the specified radix (base).

How do you convert a number variable value to a string?

The toString() method in Javascript is used with a number and converts the number to a string. It is used to return a string representing the specified Number object. The toString() method is used with a number num as shown in the above syntax using the '. ' operator.

What method converts numeric values into string values?

The toString() method is a built-in method of the JavaScript Number object that allows you to convert any number type value into its string type representation.


2 Answers

you can use ''+your number

let num:number=3;
let numTxt:string=''+number;
like image 159
Eliseo Avatar answered Sep 30 '22 15:09

Eliseo


Integer to string is as simple as calling .toString();

As @GünterZöchbauer said TypeScript has the same type coercion as JS, so numbers are automatically converted to strings when used in a string context

So no problem will occur same as Javascript, you can use number as string.

If you want you can do it like this so :

let num = 3;//number
let stringForm = num.toString();
console.log(stringForm);
like image 22
Saurabh Agrawal Avatar answered Sep 30 '22 13:09

Saurabh Agrawal