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, ''));
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:
Regex to check whether a string contains only numbers [duplicate]
(21 answers)
Closed 4 years ago.
I want a Javascript regex to replace only numbers i.e. all others alphabets and special characters are allowed.
This should do:
let string= "26kgsl5"
let newString = string.replace(/[0-9]/g, "");
console.log(newString);
This question already has answers here:
How to add two strings as if they were numbers? [duplicate]
(20 answers)
Closed 4 years ago.
So im quite new to javascript and i tried making something simple as entering a value and add 150 to it but it wont show number + 150?
Pictures below
Code
Output
That's because you are concatenating strings. You need to convert the strings to integers by using parseInt(), and then add the numbers.
https://www.w3schools.com/jsref/jsref_parseint.asp
var fullprice = parseInt(price) + 150;
Because you're actually concatenating a string with a number. The input value is a string, so before operating with it, parse it to int with parseInt
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt
This question already has answers here:
How to convert a currency string to a double with Javascript?
(23 answers)
Closed 6 years ago.
I have a string like this
"$2,099,585.43"
"$" maybe any symbol, like #,#..etc.
I want to convert this into 2099585.43
Is there any simple way to do this?
Use String#replace and remove characters which are not a digit or dot.
console.log(
"$2,099,585.43".replace(/[^\d.]/g, '')
)
This question already has answers here:
Regular expression for extracting a number
(4 answers)
Closed 6 years ago.
I have the following string:
PR-1333|testtt
I want to get the number 1333 using regular expressions in javascript. How can I do that?
This will look for numbers in your text.
var text = "PR-1333|testtt";
var number = text.match(/\d+/g); // this returns an array of all that it found.
console.log(number[0]); // 1333