Returns a value as a string value appropriate to the host environment's current locale.
function toLocaleString() : String |
Remarks
For the Array object, the elements of the array are converted to strings and these strings are concatenated and returned, each separated by the list separator specified for the host environment's current locale.
For the Date object, the toLocaleString method returns a String object that contains the date written in the current locale's long default format.
-
For dates between 1601 and 9999 A.D., the date is formatted according to the user's Control Panel Regional Settings.
-
For dates outside this range, the default format of the toString method is used.
For the Number object, toLocaleString produces a string value that represents the value of the Number formatted as appropriate for the host environment's current locale.
For Object objects, ToLocaleString is provided to give all objects a generic toLocaleString capability, even though they may not use it.
Note |
---|
toLocaleString should only be used to display results to a user; it should never be used as the basis for computation within a script as the returned result is machine-specific. |
Example
The following client-side example illustrates the use of the toLocaleString method using an Array, a Date, and a Number object.
В | Copy Code |
---|---|
function toLocaleStringArray() { // Declare variables. var myArray = new Array(6); var i; // Initialize string. var s = "The array contains: "; // Populate array with values. for(i = 0;i < 7; i++) { // Make value same as index. myArray[i] = i; } s += myArray.toLocaleString(); return(s); } function toLocaleStringDate() { // Declare variables. var d = new Date(); var s = "Current date setting is "; // Convert to current locale. s += d.toLocaleString(); return(s); } function toLocaleStringNumber() { var n = Math.PI; var s = "The value of Pi is: "; s+= n.toLocaleString(); return(s); } |