14

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

3
  • 1
    are you saying what you do gives you an error? if so, what is the error? Commented Nov 7, 2017 at 11:02
  • 2
    TypeScript has the same type coercion as JS, so numbers are automatically converted to strings when used in a string context. Commented Nov 7, 2017 at 11:04
  • I have closed this topic , got a clear idea from Gunter and Saurabh, thanks for the valuable comments Commented Nov 7, 2017 at 12:20

5 Answers 5

20

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 :

const num = 3;//number
const stringForm = num.toString();
console.log(stringForm);

Sign up to request clarification or add additional context in comments.

Comments

4

Pls try

text:  `${this.errorMsg} Energy produced in the Energy`,

NB: Use `` (back-ticks) , instead of '' (apostrophe), to get your value.

Comments

4

you can use ''+your number

let num:number=3;
let numTxt:string=''+number;

Comments

1

It will be converted into the string automatically, for an example

class myClass{
    private myNum: number = 10;
    private myName: string = "Name Surname";
    
    onInit() {
        console.log(this.myName+this.myNum);    
    }
}

will be converted into these lines when transpiled

var myClass = /** @class */ (function () {
    function myClass() {
        this.myNum = 10;
        this.myName = "Name Surname";
    }
    myClass.prototype.onInit = function () {
        console.log(this.myName + this.myNum);
    };
    return myClass;
}());

that you can check in typescript website under play ground section,

Demo

Comments

0

let num = 3;//number
let stringForm = num.toString();
console.log(stringForm);

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.