Copy link to clipboard
Copied
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.
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
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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!
Copy link to clipboard
Copied
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
}
Copy link to clipboard
Copied
Thanks, Rob, but the link does not add something new to the issue. Your code is still a limited custom solution. I can't cover all the variations this way. For example, cyrillic conventions often use a space as a thousands delimiter, like "123 456 789,95".
Another problem is that I need different precisions and I can't use toFixed() and the toLocaleString() at the same time as both return string and both need a number. For example:
$.locale="de"
Number(12345678.9012.toFixed(2)).toLocaleString() //gives 12345678,9 and not 12345678,90
So I think I'll stick to the user input solution:
var thousandsSeparator = "." // input from the user
var decimalPoint = "," // input from the user
var precision = 2 // input from the user
var num = 12345678.9012
num = num.toFixed(precision)
var result = num.replace(/\./g, decimalPoint).replace(/(\d)(?=(\d{3})+(?!\d))/g, '$1' + thousandsSeparator )
$.writeln(result) // 12.345.678,90
Copy link to clipboard
Copied
Rob, by the way this regex breaks on the 4 or more fraction digits, like
"12345.6789".replace(/(\d)(?=(\d{3})+(?!\d))/g, '$1,') // 12,345.6,789
Copy link to clipboard
Copied
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..
Copy link to clipboard
Copied
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?
Find more inspiration, events, and resources on the new Adobe Community
Explore Now