Javascript convert phone number from E164 to International format - javascript

Hello I have a phone number in it's E164 format : +212640588740 and I want to convert it to it's international format : +212 640-588740.
There is this library http://code.google.com/p/libphonenumber/ that do this conversion very well but it requires a phone number and a country code witch I can't provide because I'm reading the phone to convert from DB.
Basically I want a script or library that takes the E164 as arguments and turns it into it's international standard format, like the following:
+212640588740 => +212 640-588740
+33336578668 => +33 3 36 57 86 68
+17877491410 => +1 787-749-1410
Any Ideas are welcome, Thank you in Advance.

If all your input is E164 formatted, you can use com.google.i18n.phonenumbers.PhoneNumberUtil.parse() method to convert your string to PhoneNumber instance which in turn you could use com.google.i18n.phonenumbers.PhoneNumberUtil.format() to convert it to a string formatted as INTERNATIONAL.
You don't need to know the country code (or parse it) beforehand, see the params documentation of the parse() method:
defaultRegion - ... If the number is guaranteed to start with a '+' followed by the country calling code, then "ZZ" or null can be supplied.
Here's a short example:
PhoneNumber number = com.google.i18n.phonenumbers.PhoneNumberUtil.parse("+12781112222", null);
String result = com.google.i18n.phonenumbers.PhoneNumberUtil.format(number, com.google.i18n.phonenumbers.PhoneNumberUtil.PhoneNumberFormat.INTERNATIONAL);
For usage see the docs:
com.google.i18n.phonenumbers.PhoneNumberUtil.parse()
com.google.i18n.phonenumbers.PhoneNumberUtil.format()

Related

Does Dinero support parsing a money amount in a string in JavaScript?

I have a money amount in a string, eg '5.0', '1.1'
We're using dinero.js to handle money. Is there a way for Dinero to accept a string like the above
and return a money object of the equivalent amount (the currency should be one we specify, eg GBP or USD)? In the above examples, it would return £5, £1.10.
If this is supported, how?
Thanks

Converting Numbers from French Locale to English Locale JavaScript

I have a list of number like "123,459","561,79" from france region and I want to convert it into our normal US english numbering system. How can I do it in JS using locale?
function eArabic(x){
return x.toLocaleString('en-US',{ minimumFractionDigits:2,
maximumFractionDigits:2 });
}
Input : "123,345"
Output : "123,345"
Expected Output : 123.345
This doesn't looks good. Do you have any suggestion for this problem? I do not want to replace comma with '.' in order to solve this issue.
You’re passing in a string, not a Number object (which is what toLocaleString requires to produce a formatted number). Do you have the original number available?
If you don’t, then your best bet (assuming a standardised format for the original number strings) would be to convert them into normal numbers then reformat them. Assuming that your numbers are going to be formatted according to French standards (, as a decimal separator) then you could use a simple string replacement before creating your number object:
var frenchNumberString = '123,456';
var numberObject = new Number(frenchNumberString.replace(',', '.'));
Then pass numberObject into your formatting code.

Add thousand separating commas for numbers in angularJS

I have a number like $scope.numbers = 1234567
when i apply {{numbers| number : 0}}
then i got result like 1,234,567
but is this possible to get result like 12,34,567
Thanks in advance.
The number format emitted by the number filter can be changed by installing an angular locale, as described in https://docs.angularjs.org/guide/i18n
I assume the number format you want is supported by some of the Indian locales, but my knowledge of these is limited.

Formatting number in European format with two decimals

Trying to format a number to two decimals format in European culture. So that comma is decimal separator and space thousands separator.
In example 213245 should be formatted as 213 245,00
How can I do that?
213245.toFixed(2).toLocaleString();
gives 213245.00 but it should be 213 245,00
however
213245.toLocaleString()
gives 213 245
Fiddling below:
var out, input;
input = 213245;
// TEST 1
out = input.toFixed(2);
console.log(out); // 213245.00
out = out.toLocaleString();
console.log(out); // 213245.00
// TEST 2
out = input.toLocaleString();
console.log(out); // 213 245
https://jsfiddle.net/rootnode/8p2vad8u/7/
When you use Number.toFixed() you obtain a string (not a number any more). For that reason, subsequent calls to .toLocaleString() launch the generic Object.toLocaleString() method that knows nothing about numbers, instead of the Number.toLocaleString() you want.
Having a look at the documentation we can compose something like this:
> Number(213245).toLocaleString("es-ES", {minimumFractionDigits: 2});
"213.245,00"
console.log(Number(213245).toLocaleString("es-ES", {minimumFractionDigits: 2}));
This is a relatively new addition so make sure to verify browser support, but it's been working in Firefox and Chrome-like browsers for a few years now. In particular, some runtimes like Node.js do not include the full ICU dataset by default.

Format a number as phone number in MS Ajax

How to format a number as phone number (e.g. (45)879-2658 ) using MS Ajax formatting.
In C#, i can format this number using {0:(###)###-####} .
But not sure how to achieve this format in MS Ajax for an integer.
I don't know C#, this is how you can do picture formatting in javascript:
format = "(###)###-####"
input = 1234567890
formatted = format.replace(/#/g, [].shift.bind(String(input).split("")))
// result: "(123)456-7890"
To handle strings shorter than a picture, try this slightly more verbose code:
chars = String(input).split("")
formatted = format.replace(/#/g, function() { return chars.shift() || "" })

Categories