How to split country code and phone number in java script [duplicate] - javascript

This question already has answers here:
How to split mobile number into country code, area code and local number?
(7 answers)
Closed 7 years ago.
I have a number example this : - +91xxxxxxxxxx i want to split country code and mobile number.
The number is stored in a variable. and i want to split the number from end. because i am only using those country whose phone number is 10 digit long. So i can count from last and split after 10 numbers. But how to do this. I don't know.
please help.

You can try this:
const phoneNumber = '+91xxxxxxxxxx';
const countryCodeLength = phoneNumber.length - 10;
const components = {
IDDCC: phoneNumber.substr(0, countryCodeLength),
NN: phoneNumber.substr(countryCodeLength, phoneNumber.length)
};
console.log(components);

Country may have variable length code. So best possible solution would be start from back as of now we know length of mobile number is 10. once you take those 10 digits, Then you can parse country code accordingly.

Related

How can I extract number from text with regular expression [duplicate]

This question already has answers here:
Get price value from a string
(7 answers)
Closed 3 years ago.
I am trying to extract number from title. I have title like this:
**
Apartment London, 230.400€, 450,00m2
**
I want to extract only this price number (230.400€), can anybody help me with this?
So far my code look like this
\d+\.?\d+(.)
You can include the amount symbol as part of your RegEx:
/\d+\.?\d+(.)€/
Or you can try using Positive lookahead:
\d+\.?\d+(.)(?=€)

Adding two big numbers in javascript [duplicate]

This question already has answers here:
How to deal with big numbers in javascript [duplicate]
(3 answers)
Closed 3 years ago.
I've been trying to add the following numbers using javascript;
76561197960265728 + 912447736
Sadly, because of rounding in javascript it will not get the correct number, I need the number as a string.
I've tried removing the last few digits using substr and then adding the two numbers together and then putting the two strings together, sadly this doesn't work if the first of the number is a 1.
function steamid(tradelink){
var numbers = parseInt(tradelink.split('?partner=')[1].split('&')[0]),
base = '76561197960265728';
var number = parseInt(base.substr(-(numbers.toString().length + 1))) + numbers;
steamid = //put back together
}
steamid('https://steamcommunity.com/tradeoffer/new/?partner=912447736&token=qJD0Oui2');
Expected:
76561198872713464
For doing operations with such big integers, you should use BigInt, it will correctly operate on integers bigger than 2ˆ53 (which is the largest size that normal Number can support on JS
const sum = BigInt(76561197960265728) + BigInt(912447736);
console.log(sum.toString());
Edit 2020: This API still not widely supported by some browsers, so you may need to have a polyfill, please check this library

Adding ".00" When Returning Dollar Value Totals Via Math.js [duplicate]

This question already has answers here:
Format number to always show 2 decimal places
(37 answers)
Closed 4 years ago.
In my MongoDB/Node backend I am doing some totaling of dollar amounts and returning that data to the front end. Note this is for displaying totals only, we're not altering the values themselves. Because we want two decimal places for the totaled dollar values, I am doing this:
let roundedTotalOpenBalance = math.round(totalOpenBalance, 2);
That will give me something like -- 322.45 -- which is what I want.
However, if the value total is 322, I'd like to also pass that to the front end as 322.00. How can I do this with math js? Or do I need to handle the transformation myself?
Discard Math.round() and Try with toFixed()
let num1 = 322.45;
let roundedTotalOpenBalance1 = num1.toFixed(2);
console.log(roundedTotalOpenBalance1);
let num2 = 322;
let roundedTotalOpenBalance2 = num2.toFixed(2);
console.log(roundedTotalOpenBalance2);
roundedTotalOpenBalance.toFixed(2) should do the trick :)
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toFixed

Mask a input in HTML using javascript [duplicate]

This question already has answers here:
Mask US phone number string with JavaScript
(9 answers)
Closed 7 years ago.
How can I mask a HTML input text with a mask for phone number?
Here in Brazil, in some states, the number format can change.
For example:
(dd)xxxx-xxxx
this is the default number format, where dd is the state code, and the x's are the numbers.
BUT, in some states, the number nine is added to the phone number, like this:
(dd)9xxxx-xxxx. (let's call it "new format")
the var dd isn't important, what I want to do is when the user type the new format, the hyphen automatically appears after the 4th X.
When the users start entering the phone number, you could have a regex such as ^(\d{2})9 and regex match the string to see if the hyphen needs to be added at a different place.
You could use something like formatter.js to do the job for you!
I have not tried it, but the demos look quite solid.
Try using two inputs with different masking (8 digit & 9 digit respectively) and switch them based on the state selected in the form.

How to grab all numbers from a string in JavaScript? [duplicate]

This question already has answers here:
How to match multiple occurrences of a substring
(3 answers)
Closed 2 years ago.
Let's say I have an input field and want to parse all of the numbers from the submitted string. For example, it could be:
Hi I'm 12 years old.
How do I parse all of the numbers without having a common pattern to work with?
I tried:
x.match(/\d+/)
but it only grabs the 12 and won't go past the next space, which is problematic if the user inputs more numbers with spaces in-between them.
Add the g flag to return all matches in an array:
var matches = x.match(/\d+/g)
However, this may not catch numbers with seperators, like 1,000 or 0.123
You may want to update your regex to:
x.match(/[0-9 , \.]+/g)
var words = sentence.split(" ");
var numbers = words.filter(function(w) {
return w.match(/\d+/);
})

Categories

Resources