How do I convert my HEX string into a number? [duplicate] - javascript

This question already has answers here:
How to convert decimal to hexadecimal in JavaScript
(30 answers)
Closed 3 years ago.
I wish to convert the whiteHex variable to a decimal using the parseInt() function, and store it in a variable, whiteDecimal.
var whiteHex = 'ffffff';
var whiteDecimal = parseInt(whiteHex);
I am unsure if the above is correct or not. The reason being, that I then wish to subtract 1 from whiteDecimal and store it in a variable offWhiteDecimal. This is where I am getting stuck. How can I subtract one from the ffffff hex value? Am I missing something within the parseInt function?

You're looking for this:
var whiteDecimal = parseInt(whiteHex, 16)
console.log(whiteDecimal - 1);
ref: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt#Syntax

Related

Javascript rounding this number down 9703248444284653 [duplicate]

This question already has answers here:
Large numbers erroneously rounded in JavaScript
(6 answers)
Closed 1 year ago.
I'm sure this has something to do with floating point math or something, but I need to out how to correctly interpret this number 9703248444284653. Javascript apparently doesn't like it
If I just do this.
var test = 9703248444284653
console.log(test)
The result is 9703248444284652, which is obviously not correct. Problem is that 9703248444284653 is an id that can't be changed. I have no idea what to do with this.
Your value is larger than Number.MAX_SAFE_INTEGER.
console.log(9703248444284653n > Number.MAX_SAFE_INTEGER);
You should treat your IDs as BigInt values. So you can should probably store the values as strings and then parse them as BigInt values if you need to perform any math on them.
const
bigIntNative = 9703248444284653n,
bigIntConstructed = BigInt('9703248444284653');
console.log(typeof bigIntNative === 'bigint' && bigIntNative === bigIntConstructed);
console.log(`${bigIntNative}`);

Multiply a string in JS [duplicate]

This question already has answers here:
Repeat a string in JavaScript a number of times
(24 answers)
Closed 4 years ago.
i want to display a string as many times I have a generated variable. Therefore I'd like to do something like that, which doesn't work
var shower_total = 7; // this gets generated, but for simplification...
var uhrzeit = "<p class='clock_item'>Foo</p>";
document.getElementById("uhrzeit_block").innerHTML =5*uhrzeit;
That's why I tried looping it but that doesn't work neither
document.getElementById("uhrzeit_block").innerHTML =
for(b=0, b <= shower_total; b++){
uhrzeit
};
What do I do wrong or what would be a possible - beginner-compatible - solution. Thanks!
You could use String#repeat instead of a multiplication of a string. This does not work for value who can not converted to a number.
var uhrzeit = "<p class='clock_item'>Foo</p>";
console.log(uhrzeit.repeat(5));

Simple Javascript syntax behaving unexpectedly [duplicate]

This question already has answers here:
How to add two strings as if they were numbers? [duplicate]
(20 answers)
Javascript (+) sign concatenates instead of giving sum of variables
(14 answers)
Closed 8 years ago.
It is showing 60 + 5 as 605 instead of 65
g = document.getElementById("height1").value * 12;
alert(g); <---- This is showing 60
h = document.getElementById("height2").value;
alert(h); <---- This is showing 5
b = g+h;
alert(b); <---- This is showing 605
Any ideas
Javacript is not a strongly typed language. It does it's best to interpret the type of variables and generally, if you try to add a string-like variable to an integer-like variable, it will interpret it as string concatenation.
You could force the variables to be interpreted as integers like this:
b = parseInt(g) + parseInt(h);
Or, using other tricks that will force the variable to become numeric, like this:
b = (g*1) + (h*1);
Use parseInt() to convert to integer. + in Javascript is both for adding and concatenation.
how about b = (g+h)?
make sure the vars are integers, else convert them to integers
If the values of those HTML elements are strings (like in text input boxes?) then what you're doing is concatenating strings, not adding numbers.
You need to 1) be sure the input values are actually numeric, and 2) convert the string to an integer using parseInt().

Formatting Hex in Javascript [duplicate]

This question already has answers here:
How can I pad a value with leading zeros?
(76 answers)
How to output numbers with leading zeros in JavaScript? [duplicate]
(6 answers)
Closed 8 years ago.
How can I format a hex to be displayed always with 4 digits in javascript?
For example, I converting a decimal to hex :
port = 23
function d2h(d) {return (+d).toString(16);}
d2h(port)
I am successfully able to convert "23" to the hex value "17". However, I would like to be formatted like this "0017" (with four digits -- appending 0's before the 17).
All information would be greatly appreciated.
Here is an easy way:
return ("0000" + (+d).toString(16)).substr(-4);
Or:
return ("0000" + (+d).toString(16)).slice(-4);
You can use sprintf
http://www.diveintojavascript.com/projects/javascript-sprintf
I havent tried it yet but something like this should work:
sprintf("%.4X", d)

How can I convert my string to a number in Javascript? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to convert String variable to int in javascript?
I have the following:
var dataElapsed = $("#stats-list").attr("data-elapsed");
This creates a string called dataElapsed but I expect a number. How can I convert this?
Is there some way to convert with JQuery? or do I have to use some Javascript way?
Try this(assuming the data attributes have valid numeric values):
var dataElapsed = parseFloat($("#stats-list").attr("data-elapsed")); //for decimal.
or
var dataElapsed = parseInt($("#stats-list").attr("data-elapsed"), 10); //for integer.
You can use the .data() method to get values from data attributes
var dataElapsed = $("#stats-list").data("elapsed");

Categories

Resources