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 avoid scientific notation for large numbers in JavaScript?
(27 answers)
What is JavaScript's highest integer value that a number can go to without losing precision?
(21 answers)
Closed 4 years ago.
I am converting a long Sting of numbers values to number data type with Number() and parseInt() method of js but it is returning a hexadecimal value.
Does anybody know how to convert a long string values of number to Number data type?
// "1245458787546574878756756754561464567867"(type:String) to 1245458787546574878756756754561464567867 (type:Number)
var str = "1245458787546574878756756754561464567867";
console.log(Number(str)); //1.2454587875465749e+39
console.log(parseInt(str)); //1.2454587875465749e+39
console.log(parseInt(str,10)); //1.2454587875465749e+39
console.log(str.length); //40
This question already has answers here:
Javascript: Converting String to Number?
(4 answers)
Closed 4 years ago.
How to convert "4,250,000.40" to 4,250,000.40 that is converting string to number by remaining the commas and dots? using JavaScript
You can use parseFloat(str) to convert a string to a number, but first you need to remove the commas from the string, as parseFloat doesn't work for numbers with commas in them.
parseFloat(str.replace(/,/g, ""));
var str = "4,250,000.40";
str = str.replace(/\,/g, "")
console.log(str)
console.log(parseFloat(str).toFixed(2))//to always show 2 decimal places
You can't directly convert "4,250,000.40" to a number in vanilla JS, let alone preserve commas. 4,250,000.40 is not a valid number in JavaScript, because a comma is an illegal character in a Number.
you can use regex to delete commas, then use Number.parseFloat(), but then number formatting is lost. Instead, I suggest using a number formatting library like Numeral.js. To convert "4,250,000.40" to a numeral you'd use:
const num = numeral("4,250,000.40");
you can reformat your number using the format() method like so:
const formatedNum = numeral("4,250,000.40").format('0,0.00');
console.log(formatedNum); // "4,250,000.40"
Here's a working example, including more cool formatting:
const num = numeral("4,250,000.40");
const formatedNum = num.format('0,0[.]00');
console.log(formatedNum); // "4,250,000.40"
// you can format number as money
console.log(num.format('$0,0[.]00')); // $4,250,000.40
// you can use abbreviations like k or m
console.log(num.format('$0.00a')); // $4.25m
// you can use financial notation
console.log(numeral("-4,250,000.40").format('($0,0)')); // ($4,250,000)
<script src="//cdnjs.cloudflare.com/ajax/libs/numeral.js/2.0.6/numeral.min.js"></script>
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;
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