toLocaleStringBasics.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. function compare(locale, opt1, opt2) {
  6. if ( locale == opt1 ) {
  7. WScript.Echo("Passed");
  8. } else {
  9. if ( opt2 && locale == opt2 ) {
  10. WScript.Echo("Passed");
  11. } else {
  12. WScript.Echo("Failed - " + locale);
  13. }
  14. }
  15. }
  16. var sample = (1000.234).toLocaleString();
  17. var decimal = ".", thousands = ",";
  18. if (sample == "1.000,234") {
  19. decimal = ",";
  20. thousands = ".";
  21. } else if ( sample == "1000.234" ) {
  22. // we can't be sure whether system locale has thousands char or
  23. // toLocaleString has failed.
  24. if ( (1234567).toLocaleString() == "1234567" ) {
  25. // looks like it is more likely that system locale doesn't have thousands char
  26. // or there is something terribly wrong with toLocaleString implementation
  27. thousands = "";
  28. }
  29. } else if ( sample == "1000,234" ) {
  30. decimal = ",";
  31. thousands = "";
  32. }
  33. WScript.Echo ( "|| 999.9996 -> 1,000 or 1.000" );
  34. compare ( (999.9996).toLocaleString(), "1" + thousands + "000" );
  35. WScript.Echo ( "|| -999.9996 -> -1,000 or -1.000" );
  36. compare ( (-999.9996).toLocaleString(), "-1" + thousands + "000" );
  37. WScript.Echo ( "|| -1999.9996 -> -2,000 or -2.000" );
  38. compare ( (-1999.9996).toLocaleString(), "-2" + thousands + "000" );
  39. WScript.Echo ( "|| 0.9996 -> 1 or 1.00 or 1,00" );
  40. compare ( (0.9996).toLocaleString(), "1", "1" + decimal + "00");
  41. WScript.Echo ( "|| 0.1996 -> 0.2" );
  42. compare ( (0.1996).toLocaleString(), "0" + decimal + "2" );
  43. WScript.Echo ( "|| -0.1996 -> -0.2" );
  44. compare ( (-0.1996).toLocaleString(), "-0" + decimal + "2" );
  45. WScript.Echo ( "|| -0.1996 -> -0.2" );
  46. compare ( (-0.1996).toLocaleString(), "-0" + decimal + "2" );
  47. WScript.Echo ( "|| 1/3 -> 0.333 or 0,333" );
  48. compare ( (1/3).toLocaleString(), "0" + decimal + "333" );
  49. WScript.Echo ( "|| 1234567890.123456 ->1,234,567,890.123 or 1.234.567.890,123" );
  50. compare ( (1234567890.12345).toLocaleString(), "1" + thousands + "234" +
  51. thousands + "567" + thousands +
  52. "890" + decimal + "123" );
  53. WScript.Echo ( "|| 10 -> 10 or 10.00 or 10,00" );
  54. compare ( (10).toLocaleString(), "10" + decimal + "00", "10" );
  55. WScript.Echo ( "" );