Jump to content

Casting


iwato

Recommended Posts

QUESTION: How does one convert a string variable to a number variable for the purpose of numerical comparison?BACKGROUND: In Flash's ActionScript it was possible to cast a string-type variable as a number-type variable to facilitate numerical comparisons. How does one go about this in Javascript?Roddy

Link to comment
Share on other sites

You can use Number().

var x = "3";var y = "2" + x;var result = Number(y) + Number(x);alert(result); // Alerts "26"

But if you're not performing a sum (summing and concatenating use the same operator), you will be able to operate with them and the type will change automatically:

var x = "2";var y = "2" + x;var result = y * x;alert(result); // Alerts "44"

Link to comment
Share on other sites

You can use Number().Thank you for alerting me to the use of the Number() function. By way of confirmation are you saying that for all x and y whose values are numbers expressed as strings the following is true?
x > y == Number(x) > Number(y)

Roddy

Link to comment
Share on other sites

Yes, that is correct.Since Javascript is a very forgiving language, it will convert types wherever it is necessary.if ("89" < "501") is just the same as writing if(89 < 501)

Link to comment
Share on other sites

Yes, that is correct.It is a small kindness, but the sentence "Breaking any of these rules will result in a warn from an administrator, and may ultimately result in being banned." at Rings and Emeralds would read more easily, if you wrote "Breaking any of these rules will result in a warning from an administrator and the possibility of your being banned."I watched one of your games. Nicely done!Roddy
Link to comment
Share on other sites

It's usually better to convert and then check for an error before trying to use the values. You can use parseFloat and isNaN to do that.

var x = "123";var y = "xyz";x = parseFloat(x, 10);y = parseFloat(y, 10);if (isNaN(x))  alert("x is not a number");else if (isNaN(y))  alert("y is not a number");else  alert(x + y);

Link to comment
Share on other sites

It's usually better to convert and then check for an error before trying to use the values. You can use parseFloat and isNaN to do that.
var x = "123";var y = "xyz";x = parseFloat(x, 10);y = parseFloat(y, 10);

Yes, I had forgotten about the parseFloat() method, perhaps because of its complexity and specificity to more sophisticated mathematical manipulations. In any case, as Ingolme explained, when making a comparison even strings are permissible.

if (isNaN(x))  alert("x is not a number");else if (isNaN(y))  alert("y is not a number");else  alert(x + y);

So, why do you not use the try, throw and catch construct? My comparison, by the way, is made in just such a pattern, and it works great. You can use the same error message over.

Link to comment
Share on other sites

So, why do you not use the try, throw and catch construct?
Very slow. Benchmark it! :)parseFloat() isn't really just for "sophisticated mathematical manipulations", just for when you may be dealing with non-integers.
Link to comment
Share on other sites

Very slow. Benchmark it! :)
parseInt(), if you are really looking for integers rather than floats, is even faster still:
<script type="text/javascript">var start;start = new Date();for(var i = 0; i < 1000000; i++){	parseFloat(i,10);}document.write(new Date() - start);document.write("<br />");start = new Date();for(var i = 0; i < 1000000; i++){	parseInt(i,10);}document.write(new Date() - start);document.write("<br />");</script>

About 4 times faster on my machine - 400ms for one million parseFloats and 100ms for one million parseInts.

Link to comment
Share on other sites

I was actually thinking of try...catch v. conditionals :) but good point, anyway.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...