Number format with regex javascript [duplicate] - javascript

This question already has answers here:
Javascript Thousand Separator / string format [duplicate]
(15 answers)
Closed 8 years ago.
I have a number is 1205000000, I want display at 1.205.000.000
number.toString().replace(/(\d{3})/g, "$1.").toString()
but result is 120.500.000.0
I don't want reverse a number.

For the sake of correcting your regular expression (obviously for integer values only):
number.toString().replace(/(\d)(?=(?:\d{3})+$)/g, '$1.');
ยป Detailed Regex Explanation

One way would be to reverse the string before your manipulation and ther reverse it again. Like so:
var number = 1205000000;
function reverse(s) {
return s.split("").reverse().join("");
}
var str = reverse(reverse(number.toString()).replace(/(\d{3})/g, "$1."));
alert(str);
See this working fiddle.
EDIT:
See the comments. Its a bit dirty but for that specific number it will work. The link posted by #Artyom Neustroev as a comment under you question seems a whole lot better than this here.

Related

Regex after first sign [duplicate]

This question already has answers here:
How can I get query string values in JavaScript?
(73 answers)
Closed 5 years ago.
I'm gonna to write regex or other expression to get coordinates after '='.
My example:
var cords = https://maps.googleapis.com/maps/api/staticmap?center=50.082961,19.966860&zoom=13&size=300x300&sensor=false&markers=color:orange%7C50.082961,19.966860&client=gme-marktplaats&channel=bt_pl&signature=lPDQWiNQ2_mY8xgoVthZHLLYWac=
I want to get 50.082961,19.966860
I know that I could use slice but I think I could write it better with regex.
Simple base for this example: \=(.[0-9]) What's next?
Try this center\=(\d+\.\d+,\d+\.\d+)&
var val = 'https://maps.googleapis.com/maps/api/staticmap?center=50.082961,19.966860&zoom=13&size=300x300&sensor=false&markers=color:orange%7C50.082961,19.966860&client=gme-marktplaats&channel=bt_pl&signature=lPDQWiNQ2_mY8xgoVthZHLLYWac='.match(/center\=(\d+\.\d+,\d+\.\d+)&/)[1]
console.log(val)
But as other's have commented, you likely shouldn't be using regex for this purpose

Auto Reg Ex Phone Number format (xxx) xxx-xxxx [duplicate]

This question already has answers here:
How to validate phone numbers using regex
(43 answers)
Closed 6 years ago.
I found a pretty old example but it's almost doing exactly what i need to do:
Auto Dash using Javascript on FOCUS (for a telephone number format)
However instead of the current format xxx-xxx-xxxx i'd like to do (xxx) xxx-xxxx
Can someone give me a hand with the script below with this??
$('.telnumber').keyup(function() {
this.value = this.value
.match(/\d*/g).join('')
.match(/(\d{0,3})(\d{0,3})(\d{0,4})/).slice(1).join('-')
.replace(/-*$/g, '');
console.log("this value", this.value);
});
You could do something like this
var a = this.value.match(/\d*/g).join('')
.match(/(\d{0,3})(\d{0,3})(\d{0,4})/).slice(1);
this.value = ['(',a[0],') ',a[1],'-',a[2]].join('');
It's not very elegant but hey, elegance is for tailors right?
Update
Yea, I got bored and made a one-liner, here:
this.value = '('+this.value
.match(/\d*/g).join('')
.match(/(\d{0,3})(\d{0,3})(\d{0,4})/).slice(1)
.map((a,i)=>(a+[') ','-',''][i])).join('');
The code you'll want to change is .join('-'). That's where you're getting the 'xxx-xxx-xxxx' from.
You need to tokenize those 3 regular expressions and then join them separately.

javascript now to create two ways currency format? [duplicate]

This question already has answers here:
How to convert a currency string to a double with Javascript?
(23 answers)
Closed 8 years ago.
I have found this link have a simple and useable funciton to convert number to currency.
www.mredkj.com/javascript/nfbasic.html
but how to turn it back form currency to number...
I already try this :
[http://jsfiddle.net/vb2cb1tm/3/]...
But its failed...
I try to read a formatted string using javascript and make a counting on this valua...
I think something as simple as
function numbers(nStr){
return Number(nStr.replace(/[\$\,]/g,''));
}
might do the trick, though it's hard to tell without seeing your original code.
function cur2num(cur) {
return parseFloat(cur.replace(/,/g,"");
}
if currency signs try /[^0-9]\.\-]/g as first argument to the replace

Regex for a decimal number [duplicate]

This question already has answers here:
Displaying a number in Indian format using Javascript
(15 answers)
Closed 9 years ago.
I used the following regex
var x=32423332.343;
var res= x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
which gives an output of 32,423,332.343
How do I modify this regex (shortest way) to get the following output
3,24,23,332.343
Well, if you want that, you can modify your regex a bit:
\B(?=(?:\d{2})*\d{3}(?!\d))
regex101 demo
(?:\d{2})* will match even number of digits before the final \d{3}.
For PCRE engine, one that can handle integers and floating, with g enabled.
\G\d{1,2}\K\B(?=(?:\d{2})*\d{3}(?!\d))

Replace 56666.666666666664% percentage in to three digit percentage using javascript [duplicate]

This question already has answers here:
Formatting a number with exactly two decimals in JavaScript
(32 answers)
Closed 9 years ago.
I have calculated some data usign javascript and result is printing like this (56666.666666666664%). and I need to convert the result in like 56.66%. Below is a Javascript which print the above result.
$scope.totaTaken = (totalT / data.length)*100;
If you're looking for a JavaScript solution (where you do it in a controller or directive), the given comments answer your question (see toFixed). If you're looking for an AngularJS solution, where you format the value in the view, check out the number filter which was made for this!

Categories

Resources