Skip to main content
Anton Petrov
Known Participant
February 2, 2020
Answered

Number.toLocaleString() number formats

  • February 2, 2020
  • 3 replies
  • 3873 views

Hi, I'm trying to output localized numbers using the Number.toLocaleString() method. I can't find any detailed info about this methood in extendscript implementation. It seems that it partially works:

In ESTK

$.locale = "en"
$.writeln (Number("12345678.90").toLocaleString()) // output 12345678.9

$.locale = "de"
$.writeln (Number("12345678.90").toLocaleString()) // output 12345678,9

 

So it changes the decimal separator, but nothing about thousands separator. My question is - can i somehow control this method's behavior by parameters, config files, etc.

Correct answer rob day

Once you have the local string you could use the string replace() function with a regex to insert the period thousands separators:

$.locale = "de"
var reg = Number(12345678.90).toLocaleString().replace(/(\d)(?=(\d{3})+(?!\d))/g, '$1.')
$.writeln (reg) //returns 12.345.678,9

3 replies

Participant
February 10, 2023

I had the same problem.. until I selected "javascript" in File->project settings->expressions
But you say in "extendscript implementation", so you have the "legacy extendscript" selectted and that is the option  that don't work for me.
Hope you can choose the other option, because that works for me: I was tired of formating numbers changing points for commas..


Anton Petrov
Known Participant
February 16, 2023

Which IDE are you using? In the ExtendScript Toolkit 4.0.0.1 I can't find File->project settings->expressions submenu.

Does your solution changes the points-to-commas for the decimal separator, the thousands separator or both?

rob day
Community Expert
rob dayCommunity ExpertCorrect answer
Community Expert
February 2, 2020

Once you have the local string you could use the string replace() function with a regex to insert the period thousands separators:

$.locale = "de"
var reg = Number(12345678.90).toLocaleString().replace(/(\d)(?=(\d{3})+(?!\d))/g, '$1.')
$.writeln (reg) //returns 12.345.678,9
Anton Petrov
Known Participant
February 2, 2020

Thanks, Rob! This means, that the built-in method does not take care of the thousands separator, and cannot be set by the parameters, right? 

In my scenario I decided to provide a text input to the user to type a local code in the form of "de", "fr" etc. hoping that would be enough. Apparently I should take care of the thousands seprator, but I can only do this for a limited set of locales.

Your example gives my the idea, that I could offer the user 1 more field to input the desired thousands separator and use it in the replace regex. Thanks again!

rob day
Community Expert
Community Expert
February 2, 2020

This means, that the built-in method does not take care of the thousands separator, and cannot be set by the parameters, right?

 

Don‘t think so, here is the number api: http://jongware.mit.edu/Js/pc_Number.html

 

Could you make a comma delimited list of locales that use periods, and then check the lang against the list?:

 

var cList = "de,fr,gr"

var lang = "en" //returns 12,345,678.95
lang = "de" //returns 12.345.678,95

var reg;
var n = 12345678.95


if (cList.indexOf(lang) !== -1) {
    $.locale = "de"
    reg = n.toLocaleString().replace(/(\d)(?=(\d{3})+(?!\d))/g, '$1.')
    $.writeln (reg) //returns 12.345.678,95
} else {
    $.locale = "en"
    reg = n.toLocaleString().replace(/(\d)(?=(\d{3})+(?!\d))/g, '$1,')
    $.writeln (reg) //returns 12,345,678.95
}

 

brian_p_dts
Community Expert
Community Expert
February 2, 2020

It would change the thousands separator if you had commas separating the thousands in the source (123,456.78). You could also append the prototype localeString() to add those if the source data would be missing that data. 

Anton Petrov
Known Participant
February 2, 2020

Brian, thanks for your replay. But Number("123,456.78") returns NaN and Number("123 456.78") gives 123. Or am I missing something? 

Of course, I can parse the input and construct the output strings myself for a "en" and "de" locales, but I can't do this for all possible variants, like "fr", "ru", "hu", "bg"  etc.