Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 719
Sampath Avatar asked Mar 15 '18 13:03

Sampath


People also ask

What is the use of tolocalestring in JavaScript?

The toLocaleString () method is a JavaScript method available for Number and Date type values. This method allows you to create a representation of the values in a locale-sensitive format. The locale values can be customized using the locales and options arguments to fit your requirements.

Why can't I compare tolocalestring () to a static value?

Most notably, the IE and Edge browsers insert bidirectional control characters around dates, so the output text will flow properly when concatenated with other text. For this reason, you cannot expect to be able to compare the results of toLocaleString () to a static value: Note: See also this StackOverflow thread for more details and examples.

Is it possible to use tolocalestring () in iOS but not in Android?

Using number.toLocaleString ('fa') works perfectly in iOS but not in android. Sorry, something went wrong. Hi all, I am also experiencing this issue on android although it works fine in IOS.

What does the method tolocalestring() return?

The toLocaleString() method returns a string with a language-sensitive representation of this number. The source for this interactive example is stored in a GitHub repository. The compatibility table in this page is generated from structured data.


1 Answers

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()
like image 117
Asons Avatar answered Sep 28 '22 17:09

Asons