1

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

Dharman
  • 30,962
  • 25
  • 85
  • 135
Azhar
  • 11
  • 3

1 Answers1

1

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);

DoubleJG
  • 85
  • 4