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

1
  • Why are you subtracting 0.1? Where did 4999.17 come from - did you mean 4990.17? Commented Sep 4, 2020 at 20:32

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

2 Comments

Shouldn't the radix, in this case, be 16?
Yes it should be sorry, updated the answer now. Not sure how that slipped

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.