This question already has answers here:
Strip all non-numeric characters from string in JavaScript
(12 answers)
Closed 8 years ago.
I get a number format string from an input box like: 1,033.00
How can I convert it to 1033 by jquery/javascript?
I have to use this converted number to add it with another number.
You can do a string replace numberString= numberString.replace ( /[^0-9]/g, '' );
To add it to another number you can use + operator to convert the numberString to a number value.
var numberString= numberString.replace ( /[^0-9]/g, '' );
var addition = +numberString + anotherNumber;
Related
This question already has answers here:
Convert currency to decimal number JavaScript
(3 answers)
Closed 9 months ago.
How to convert R$ 800,000.00 in string type to 800000 decimal type using JavaScript?
Why don’t you just use
currenyValueInt = parseInt(currencyValueString.split(" ")[1])
You sanitize the string by replacing everything but digits and the decimal separator and then cast it to a Number, either with Number() or by simply adding a + in front of the expression:
let str = +"R$ 800,000.00".replace(/[^\d.]/g, '');
console.log(str, typeof str)
This question already has answers here:
How to convert decimal to hexadecimal in JavaScript
(30 answers)
Closed 2 years ago.
I have a variable like this:
var currency = "4,990.17"
currency.replace(/[$,]+/g,"");
var currency2 = parseDouble(currency)-0.1;
How can I set currency2 to be hexadecimal with 0x in front of it?
I would like my string hexadecimal value of 4999.17 to become:
0x137E.2B851EB851EB851EB852
Once converted to a number you can call .toString([radix]) ( MDN Docs ) with an optional radix which is in the range 2 through 36 specifying the base to use for representing numeric values.
var currency = 4990.17;
hexCurrency = currency.toString(16);
console.log(hexCurrency);
This returns 137E.2B851EB851EB851EB852 or you can add 0x by doing
hexCurrency = "0x" + currency.toString(16);
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:
How to format numbers as currency strings
(67 answers)
How to format a number with commas as thousands separators?
(50 answers)
Closed 9 years ago.
I want to convert my JavaScript number into a currency number, but with any currency symbol
Suppose this is my number:
var number = 43434;
The result should be like this:
43,434
And not this:
$43,434
Using one regex /(\d)(?=(\d{3})+(?!\d))/g:
"1234255364".replace(/(\d)(?=(\d{3})+(?!\d))/g, "$1,");
"1,234,255,364"
To achieve this with an integer you can use +"" trick:
var number = 43434;
(number + "").replace(/(\d)(?=(\d{3})+(?!\d))/g, "$1,"); // 43,434