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);
Related
This question already has an answer here:
Keep trailing or leading zeroes on number
(1 answer)
Closed 9 months ago.
I have some string values like "35.5" , "32.20" and I want them to be converted to numbers but keep the exact same decimals. When I use Number("32.0") for example I get 32 but I want 32.0. If I convert Number("35.5") I want 35.5 not 35.50, is there any way to do this easily?
if you want a fixed number of floating point you can use toFixed but be aware that returns a string
const strings = [ "35.5" , "32.20", "32.0"]
const result = strings.map(n => parseFloat(n).toFixed(1))
console.log(result)
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:
Is there a JavaScript function that can pad a string to get to a determined length?
(43 answers)
Closed 6 years ago.
Take this IP address:
192.168.1.1
I want to break it down into 4 full binary octets, so:
11000000 . 10101000 . 00000001 . 00000001.
All of the conversions I know, and those that I've found on other stackoverflow questions, only return the binary number itself, for example:
(1 >>> 0).toString(2) returns 1 when I want 00000001
Number(2).toString(2) returns 10 when I want 00000010
Is there an in-built javascript method that I haven't come across yet or do I need to manually add the 0's before depending on the number?
You can use Number#toString method with radix 2 for converting a Number to corresponding binary String.
var str = '192.168.1.1';
console.log(
// Split strings based on delimiter .
str.split('.')
// iterate over them
.map(function(v) {
// parse the String and convert the number to corresponding
// binary string afterwards add preceding 0's
// with help of slice method
return ('00000000' + Number(v).toString(2)).slice(-8)
// rejoin the string
}).join('.')
)
The simplest solution is to prefix the answer with zeros and then use a negative substring value to count from the right, not the left...
alert(("00000000" + (1 >>> 0).toString(2)).substr(-8));
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