This question already has answers here:
How to format a number with commas as thousands separators?
(50 answers)
Closed 8 years ago.
How to add commas to numbers, presently I'm producing an output like this 1,2,3,4,5,6,7,890 - trying to have a result that outputs the following 1,234,567,890 - using keyup which might cause issues, please advise
numberWithCommas : function () {
var goal = $("#foo");
goal.val(goal.val().toString().replace(/\B(?=(\d{3})+(?!\d))/g, ","));
},
Update:I found that replace(/\B(?=(\d{3})+(?!\d))/g, ','); fixed the issue of too many commas
remove all the current commas, then insert new ones in the appropriate places :
numberWithCommas : function () {
$("#foo").val(function(_,val) {
return val.replace(/\,/g,'').replace(/\B(?=(\d{3})+(?!\d))/g, ',');
});
},
FIDDLE
Related
This question already has answers here:
How to format a number with commas as thousands separators?
(50 answers)
Closed 3 years ago.
Is there a way to use the below JavaScript code to use comma's? For example the var num = 1924.00 Is there way to get it to display as 1,924.00?
var num = parseFloat(totalAmount).toFixed(2);
You could use a specific regex like this one in the function below:
function format_currency( value ) {
return value.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}
If you want to use it in conjuction with the .toFixed(), just do like this:
format_currency( 1245.3.toFixed(2) );
Hope it helps.
This question already has answers here:
Remove commas from the string using JavaScript
(3 answers)
Closed 4 years ago.
How could I transform a number with commas like 123,245 to a number without commas like this 123245, I know that you could already put commas in number without them but how to do that in reverse?
pure JAVASCRIPT please!
var number = Number("123,456".split(",").join(""));
Or:
var number = parseInt("123,456".split(",").join(""));
An alternative is using a regex /,/g
console.log("123,245".replace(/,/g, ''));
console.log("123,245,566".replace(/,/g, ''));
This question already has answers here:
how to extract floating numbers from strings in javascript
(3 answers)
Closed 6 years ago.
I have a string in javascript:
ECTS: 7.5 pts
From this string I need to extract 7.5 and parse it to a float value..
I've tried searching, but none of the solutions I found did the trick. I've tried:
var regex = /^\d+\.\d{0,3}$/;
var string = "ECTS: 7.5 pts";
var number = parseFloat(string.match(regex));
console.log("number is: " + number);
I've tried with a couple of different regular expressions, but none of them have done the trick.
I've created this fiddle to illustrate the problem.
EDIT1
I used to have this /\d+/ as my regex, but that didn't include floating point numbers.
EDIT2
I ended up using this as my regex /[+-]?\d+(\.\d+)?/g
Updated fiddle
This works nicely, no need for regex:
var s = "ECTS: 7.5 pts";
var n = parseFloat(s.split(" ")[1]);
$("#test").text(n);
did you try this?
var digits = Number((/(\d+\.\d+)/.exec('ECTS: 7.5 pts') || []).pop());
This question already has answers here:
How to round to at most 2 decimal places, if necessary
(91 answers)
Closed 8 years ago.
I want to format a number to two decimal places. Say the user enters 8764444 it should be formatted as 8.76. is there some built-in function in javascript to do that?
No, there is no built in method for exactly that, but you can use the substr method to get parts of a string to do the formatting:
var input = "8764444";
input = input.substr(0, 1) + '.' + input.substr(1, 2);
// show result in Stackoverflow snippet
document.write(input);
This question already has answers here:
Simple regular expression for a decimal with a precision of 2
(17 answers)
Closed 9 years ago.
In Javascript, I am trying to validate a user input to be only valid decimals
I have the following JSFiddle that shows the regex I currently have
http://jsfiddle.net/FCHwx/
var regex = /^[0-9]+$/i;
var key = '500.00';
if (key.match(regex) != null) {
alert('legal');
}
else {
alert('illegal');
}
This works fine for integers. I need to also allow decimal numbers (i.e. up to 2 decimal places)
I have tried many of the regex's that can be found on stackoverflow
e.g.
Simple regular expression for a decimal with a precision of 2
but none of them work for this use case
What am I doing wrong?
This should be work
var regex = /^\d+(\.\d{1,2})?$/i;
Have you tried this?
var regex = /^[0-9]+\.[0-9]{0,2}$/i;
I recommend you to not use REGEX for this, but use a simple !isNaN:
console.log(!isNaN('20.13')); // true
console.log(!isNaN('20')); // true
console.log(!isNaN('20kb')); // false
Try this:
\d+(\.\d{1,2})?
d is for digit
d{1,2} is for 1 digit before . and at least 2 digits such as 0.51