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() || "" })
Related
I wrote a Javascript method that will take a decimal number input and gives a binary number in the form of string.
Example 1:
Input: 15
Output : "1111"
The method I wrote works as follows:
If my input number is 15,
var t = parseInt(15,10).toString(2);
then output of t will be "1111"
My question is how to convert a number lesser than 15 also as a 4-bit binary number string.
As in,
Input: 4
Desired output : "0100", but what I get right now is, "100".
Please help me with the workaround.
Use padStart
The padStart() method pads the current string with another string (multiple times, if needed) until the resulting string reaches the given length. The padding is applied from the start of the current string.
var t1 = parseInt(4, 10).toString(2).padStart(4, "0")
var t2 = parseInt(15, 10).toString(2).padStart(4, "0")
console.log(t1)
console.log(t2)
I am trying to parse a hex value to decode a card
The hex data I receive from the card is f8b2d501f8ff12e0056281ed55
First I am converting this to an integer with parseInt()
var parseData = parseInt('f8b2d501f8ff12e0056281ed55', 16);
The value recieved is 1.9703930145800871e+31
When I try to decode this using the bitwise operator in Javascript
var cardNumber = ((parseData & 0xFFFFF) >> 1).toString();
I received a 0 value.
What am I doing wrong here, how can I parse the value of such large integer number?
There are two ways to do it:
First, notice that & 0xFFFFF operation in your code is just equivalent to getting a substring of a string (the last 5 characters).
So, you can just do a substring from the end of your number, and then do the rest:
var data = 'b543e1987aac6762f22ccaadd';
var substring = data.substr(-5);
var parseData = parseInt(substring, 16);
var cardNumber = ((parseData & 0xFFFFF) >> 1).toString();
document.write(cardNumber);
The second way is to use any big integer library, which I recommend to you if you do any operations on the large integers.
Since the number integer is so big you should use any bigNum library for js.
I recommend BigInteger, since you are working only with integers and it supports bitwise operations, however you can also check this answer for more options.
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.
I need to fetch the currency from a string. My price can have multiple format. Let's take for example CAD vs USD. CAD will be formatted this way :
5.00$ CAD
While USD will be formatted this way
$5.00 USD
The system also support euro, and GBP. Obviously the price could be in the tousands.
Here is my unsuccessful attempt :
result = /^([^\$]+)\$/i.exec(price.text());
Don't overthink it.
/(\d|\.)+/
will pull out the number.
You can also do a replace, per adeneo, to clean it up:
price.replace(/[^0-9.]/g, '')
and then parseFloat if you need to.
Wow - I'm not sure I understand - you only want to extract the number (thousands, wholes and decimal portions) buit not the type of currency??? what about exchange rates?? are there other columns that indicate the currency type? just seems weird...
But if the column only contains a string with the amnount and currency indicators, then [\d.,]+ should get the number (and any trailing or leading commas or periods) - does that work for you?
Money is commonly represented as an [int, currency] tuple. I'd aim towards a format like that.
Your string seems to have a bit of fluff in it. The $ for one.
So how about:
var db_string = "5.00$ CAD"; // or "$5.00 USD" or "€5.00 EUR"
var clean = db_string.replace(/[0-9A-Z ]/g, ""); // removes $ . and euro etc.
var parts = clean.split(" ");
var num = parseInt(parts[0], 10); // amount as int, in cents
var cur = parts[1]; // currency: USD, CAD, EUR etc.
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()