Click to See Complete Forum and Search --> : Converting string to number


shraddhab
October 23rd, 2002, 04:26 AM
Hello All,

I am facing problem in converting string to number.

I have two strings...

strFrom = "123456789123456789123";
strTo = "123456789123456789130";

I converted them into integers......by using parseInt and Math.abs

But output of parseInt("123456789123456789123") is not proper integer output. Same is happening with Math.abs.

The difference of these two strings is supposed to be 7............. after converting into integers

but i am not able to get that................

can anyone help me...............

Thanks in advance..

Shraddha Biyani

Metaphor
October 23rd, 2002, 10:00 AM
Well. This 'integer' is far too large to be contained in any programming language's interger datacarrier. I assume you're using Java, from the functions you mentioned. Try using a double, instead, but I think even that's too small. Why the huge number?

Waldo2k2
October 23rd, 2002, 12:18 PM
it's not java, it's javascript, very different.

there is no double type in javascript....there is no integer type, the browser does the deciding, you just declare
var someVariable;

try this

<html>
<body>
<script language="javascript">
//can only handle a 16 digit number...i suggest breaking apart somehow then working with peices of it.
var first="9999999999999999";
var second="9999999999999998";
var third=first-second;
//or var third=parseInt(first)-parseInt(second); both have same result

alert(third);
</script>
</body>
</html>

i tested it and it works fine

Metaphor
October 23rd, 2002, 12:27 PM
Disregarding it being javascript, and there not being any datatypes in said language, the number was still too big. ;p

Waldo2k2
October 23rd, 2002, 12:42 PM
yes, that's why i noted it in my sample code.
beyond that, he was doing it wrong anyway, a string and an integer are interchangeable in javascript (as long as they are numeric characters)

Metaphor
October 23rd, 2002, 12:43 PM
Really? Useful.