Search code examples
javascriptangulartypescript

toLocaleString() is not working


Update:

enter image description here

Original:

Could you tell me why toLocaleString() is not working here? It works in all other places. I need it to give 1000 separators. Any clue or work around?

 forEach(budgetList, async (b: Budget) => {
          const budget: Budget = find(this.oldBudgetList, (budget: Budget) => {
            return b.budgetGroup.name == budget.budgetGroup.name;
          });
          if (budget == null) {//new item
            const log = this.logProvider.getLogDetails(`Budget : ${b.budgetGroup.name}`, `Added ${b.amount.toLocaleString()}`, 'project', project.id);
            await this.logProvider.createLog(log, project.id);//add log
          } 
        });

Runtime:

enter image description here

Another place and works well!

const log = this.logProvider.getLogDetails('Project : Budget', `Budget changed from ${this.oldBudgetValue.toLocaleString()} to ${project.budget.toLocaleString()}`, 'project', project.id);
await this.logProvider.createLog(log, project.id);//add log

Solution

  • If the value is of type String, you could do e.g.

    Number(b.amount).toLocaleString()
    

    (which was what was working in OP's case)


    Other options could be

    parseInt('number as string').toLocaleString()
    
    parseFloat('number as string').toLocaleString()