How do I contribute Internationalization API languages or add language ? In Mongolia we use different number formatting and date format, also when I need to format number in Mongolian I need to create custom function to do it. And I want to use Intl API, because
const formattedNumber = Intl.NumberFormat('mn-MN', { notation: "compact" })
is better than writing function below
function formatNumber (number) {
if(number > 1000000000) return number / 1000000000 + " тэрбум"
///and so on
return number;
}
I did little research and all the languages and variants are registered here and subtags are listed in the Unicode CLDR Project. But when I use mn-Cyrl or any other Mongolian locale it returns default en-US string. So I am wondering if I am able to contribute to Intl API localization.
I see, you might need this. it's official
proposal-intl-numberformat-v3
Related
I was wondering why the Kotlin JavaScript Date class returns a Double for the getTime function. According to the documentation, the getTime function should return the number of milliseconds since 1 January 1970 00:00:00 UTC.
I know that JS doesn't have a 64 bit numeric representation, but since Kotlin emulates Longs I feel like the value returned by Date.now() and Date().getTime() should be a Long. At the very least it would make more sense to return an Int.
Is there any reason that it returns a Double instead of a whole number?
There are two separate reasons for that: consistency and performance.
But before I get into that please note that you can easily get the desired behavior via extensions, e.g.:
inline val Date.time get() = getTime().toLong()
println(Date().time) // 1522757176433
Consistency. As you can see in the Date documentation page, it an external class. Which means it just describes an existing JavaScript API. Is it a good design decision to reuse it as it? Maybe not. But it has a benefit of being familiar to the JS folks. Changing the return type of a single function ruins that and makes the whole API quite inconsistent.
Performance. It is possible hide the original getTime and create a helper function instead. But keep in mind that in order to emulate Long Kotlin creates an object with 2 Number's. Creation, comparison, binary operations, storage - emulated Long's perform a lot worse than native JS Numbers.
Summing up. Changing the return type would make the API inconsistent and inefficient. If you value semantics over performance, just write a few helper functions.
P.S. I think the Date API will be redesigned at some point to make it possible to use it in multiplatform projects. But that's another story.
In kotlin use Date().time .It will return the long Values you will get whole number
val s= Date().time
print(s)
for example
val date = "01-02-2018 07:05:00.999"
val fmt = SimpleDateFormat("MM-dd-yyyy HH:mm:ss.S") //parse date based your format
var myDate: Date? = null
try {
myDate = fmt.parse(date)
} catch (e: ParseException) {
e.printStackTrace()
}
println(myDate)
val timestamp = myDate!!.time //timestamp values in long only not double
println(timestamp)
I'm looking for a complete list of all the different Intl.NumberFormats.
The Intl.NumberFormat page shows 3 different formats.
ja-JP -> "¥123,457"
de-DE -> 123.456,79 €
en-IN -> 1,23,000
Is this all? Or is there more?
Intl was introduced here https://hacks.mozilla.org/2014/12/introducing-the-javascript-internationalization-api/ with these takeaways:
There's no official list, since "The api is best-effort"
By default, browsers accepts ALL correctly formatted codes (BCP 47 is mentioned and then finds the best fit
You might specify lookup only to disable the best fit algorithm, and then you could get a list of all countries and language codes and that way run through all combinations for a specific browser on a specific OS with a specific list of installed languages but I guess that's not what you want to hear if you're building a web project. But you might want to make use of supportedLocalesOf to have something locally relevant to choose from.
Under the hood browsers typically use ICU or similar
tl;dr: So if you send in your best guess at a valid locale, the best fit algorithm will get you something usable!
Valid locale?
According to the documentation and my earlier research, the Intl javascript API expects you to format your locale codes according to BCP 47.
Go through this it has all the format available
Some of popular locales:
pl_PL = 1 205,34 zł
en_US = $1,205.34
en_GB = £1,205.34
en_IE = €1,205.34
de_DE = 1.205,34 €
fr_FR = 1 205,34 €
br_FR = € 1 205,34
ja_JP = ¥1,205
pt_TL = 1 205,34 US$
fr_CA = 1 205,34 $
en_CA = $1,205.34
And for All possible locales visit:
https://gist.github.com/ncreated/9934896
en-IE = €1,205.34
ro-MD = 1.205,34 MDL
br = ¤ 1 205,34
en-GY = $1,205
es-GT = Q1,205.34
All Locale Visit : https://gist.github.com/raushankrjha/d1c7e35cf87e69aa8b4208a8171a8416
All different formats available worldwide.
$c = '{
"en_EN": "$100,000.00",
"de_DE": "100.000,00 $",
"af_AF": "$ 100,000.00",
"am_AM": "US$100,000.00",
"ar_AR": "١٠٠٬٠٠٠٫٠٠ US$",
"bn_BN": "১,০০,০০০.০০US$",
"bg_BG": "100000,00 щ.д.",
"ca_CA": "100.000,00 USD",
"cs_CS": "100 000,00 US$",
"nl_NL": "US$ 100.000,00",
"et_ET": "100 000,00 $",
"fr_FR": "100 000,00 $US",
"he_HE": "100,000.00 $",
"hi_HI": "$1,00,000.00",
"it_IT": "100.000,00 US$",
"nb_NB": "USD 100 000,00",
"ms_MS": "USD100,000.00",
"id_ID": "US$100.000,00",
"pl_PL": "100 000,00 USD"}';
I'm trying to use the new Javascript internationalization API, and would like to know if there is a way to get the decimal and thousands (grouping) separator for a Intl.NumberFormat instance?
There is a resolvedOptions method on the object, but that does not provide the symbols.
In case anybody's wondering, then for en-US, these would be a comma , and period ., such as in 1,000.00.
If nothing else, as a trick solution (that doesn't pass the Turkey Test; see comment), you can use the output of toLocaleString() to determine information about number formatting in a locale. For the current locale, for example:
var decimalSeparator =
(12345.6789).toLocaleString().match(/345(.*)67/)[1];
var thousandSeparator =
(12345.6789).toLocaleString().match(/12(.*)345/)[1];
var numberOfDecimals =
(12345.6789).toLocaleString().match(/345(\D*)(\d+)$/)[2].length;
The same trick can be used for currency formatting, using e.g. (12345.6789).toLocaleString("en-GB", { style: "currency" }).
I'm afraid ECMA-402 standard does not define the API that let you access separators. There is also another problem - at the moment the adoption of ECMA-402 is not as wide as we wish.
Therefore for the time being, if I were you I would look to something like CLDR JSON bindings or iLib which apparently provides these information through LocaleInfo's getDecimalSeparator() and getGroupingSeparator() functions.
BTW. iLib seems to use CLDR as a source of information.
I am working with kibana (elasticsearch dashboard) which allow to specify a date pattern to explain the index naming pattern.
For instance, default pattern is: [logstash-]YYYY-MM-DD.HH
I'd like to organize my index by block of hours, let's say by block of 4 hours. Indices would then be named logstash-2014-02-25.00, logstash-2014-02-25.04, logstash-2014-02-25.08, …
Is there any way to get such format with momentjs ? I am dreaming of [logstash-]YYYY-MM-DD.{HH%4}
but the documentation does not explain such thing (how weird).
It seems impossible to do it with formatting primitives.
However, I found we can override some of the language-dependant formatter such as meridiem formatter.
A solution would then be to insert somewhere (in kibana conf file for example):
moment.lang('fourHourblocks', {
meridiem: function(hour, minute, isLowercase) {
hourBlock = Math.floor(hour / 4).toString();
if (hourBlock.length < 2) hourBlock = "0" + hourBlock;
return hourBlock;
}
}
then I can set the pattern to [logstash-]YYYY-MM-DD.A.
This is kind of hacky so I am still opened to other solutions
I want to format number using javascript as below:
10.00=10,00
1,000.00=1.000,00
Every browser supports Number.prototype.toLocaleString(), a method intended to return a localized string from a number. However, the specification defines it as follows:
Produces a string value that represents the value of the Number formatted according to the conventions of the host environment's current locale. This function is implementation-dependent, and it is permissible, but not encouraged, for it to return the same thing as toString.
Implementation-dependant means that it's up to the vendor how the result will look, and results in interoperability issues.
Internet Explorer (IE 5.5 to IE 9) comes closest to what you want and formats the number in a currency style - thousands separator and fixed at 2 decimal places.
Firefox (2+) formats the number with a thousands separator and decimal places but only if applicable.
Opera, Chrome & Safari output the same as toString() -- no thousands separator, decimal place only if required.
Solution
I came up with the following code (based on an old answer of mine) to try and normalize the results to work like Internet Explorer's method:
(function (old) {
var dec = 0.12 .toLocaleString().charAt(1),
tho = dec === "." ? "," : ".";
if (1000 .toLocaleString() !== "1,000.00") {
Number.prototype.toLocaleString = function () {
var neg = this < 0,
f = this.toFixed(2).slice(+neg);
return (neg ? "-" : "")
+ f.slice(0,-3).replace(/(?=(?!^)(?:\d{3})+(?!\d))/g, tho)
+ dec + f.slice(-2);
}
}
})(Number.prototype.toLocaleString);
This will use the browser's built-in localization if it's available, whilst gracefully degrading to the browser's default locale in other cases.
Working demo: http://jsfiddle.net/R4DKn/49/
I know this solution using NumberFormat but it is necessary to convert the values to string.
https://developer.mozilla.org/es/docs/Web/JavaScript/Referencia/Objetos_globales/NumberFormat
// Remove commas
number = "10,000.00".replace(/,/g, '');
// Create a NumberFormat type object
var formatter = new Intl.NumberFormat('de-DE', {
minimumFractionDigits: 2
});
// Apply format
console.log(formatter.format(number));
output:10.000,00
Javascript doesn't provide this functionality itself, but there are a number of third-party functions around which can do what you want.
Note, whichever method you use, you should be careful to only use the resulting string for display purposes -- it won't be a valid number value in Javascript after you've converted the decimal point to a comma.
The quickest solution I can offer is to use the number_format() function written by the phpJS people. They've implemented Javascript versions of a load of commonly-used PHP functions, including number_format(), and this function will do exactly what you want.
See the link here: http://phpjs.org/functions/number_format
I wouldn't bother about taking the whole phpJS library (a lot of it is of questionable value anyway), but just grab the 20-odd line function shown on the page linked above and paste it into your app.
If you want a more flexible solution, there are a number of JS functions around which simulate the printf() function from C. There is already a good question on SO covers this. See here: JavaScript equivalent to printf/string.format
Hope that helps.