Why is this basic calculation not working?

EXAMPLE 1


a = 98587
b = 0711

c = a - b

This results with c = 98130 which is wrong it should be 97876.  

EXAMPLE 2


a = 98587
b = 07

c = a - b 

This results with c = 98580 which is correct.

So why does the first calculation come out incorrectly? This is a very basic calculation and just trying to figure out why its not computing correctly…Thanks.

0711 = 457 in decimal, remove the zero, and it should work fine.

H88,

Thanks for the response. Is there a way to specify not to convert it to decimal? The problem is var ‘b’ in my example is input by user based on a serial number and can contain a leading zero and be anywere between 1-5 digits. If not I suppose I can do a if statement and determine if the number contains a leading zero then remove it.

Thanks!

This turned out to be more difficult than I thought.

a = 98587
b = parseInt(“0711”, 10)
c = a - b

Returns with 89. That works however b is declared from input field so i cant figure out how im going to declare it as base 10 from the get go. Any suggestions? Why the heck does flash automatically convert numbers to decimal in certain cases?

Hey… Converting it to decimals is way more convenient than taking the zero out like other programming languages do…

I’d suggest making it so that the serials don’t start off with 0’s in the beggining… Unless this is imperative… In that case… Have fun… lol

Seriously though… I’d suggest just try and get rid of the 0 leader thing.

playamarz,

I will have to somehow check for a leading zero and remove it. The serial will not always contain a leading zero but it can. Im not sure how to go about searching the string and removing the leading zero before I make my calculations however. Any suggestions?

I don’t know if flash has these toools in it… (haven’t checked yet)… But I know a way it can be done if they do have a tool for it…

Load up the variable as a string and not an int at first… Then search the string for “0” and take it out if needbe… Then convert the number to an integer…

Make sense then? I know C++ has a couple of tools to do this… Alot of languageshave methods of truncating strings… JavaScript, PHP, ASP, and C++ are the ones that I remember… I’m sure ActionScript has somehting in it…

I’ll see if I can figure somehting out for you while you wokr on it as well. :+)

Well I found out that the code below will check the first digit in my string. Working on how to delete it if its a ‘0’ now.


check = myString .charAt(  0  ) 

The problem with your calculation is that when Flash sees a number starting with 0, it assumes you’re using the octal number system (similariy, if it sees 0x, then it assumes you’re using hexidecimal). Anyway, the simplest way to fix your problem is to use the parseInt function. This is how you would use it, assuming your number is stored in the variable called myNumString, but as a string (this is important!):


myNum = parseInt(myNumString, 10);

This will basically take the string myNumString, and parse it into an integer using the base 10 (or decimal) number system. Problem solved.

BTW, this works in F5 or later.

-Al

Alethos,

Thanks for the reply. I have been trying to get it wokring using parseInt but having problems. Mind taking a look at my FLA?

[COLOR=red]Download FLA[/COLOR]

Heya.

I downloaded your FLA and checked it out. Simply replace the code you have in frame 1 with the following and it works.


enter.onRelease = function() {
	// Convert the input from the input text field (variable: input) to a string.  
	String(input);
	//  Parse the string, converting it to a decimal number, then output to dynamic text box (variable: output).
	output = parseInt(input, 10);
};

-Al

Al,

Thanks that worked. So how would I code that if I wanted to change ‘input’ to another var to be called up later by the function. i tried the following code which seems to follow the same logic but it didn’t work.


String(input);
test = parseInt(input, 10);


enter.onRelease = function() {
    output = test
};

Thanks for your help!

Ok, I’ll describe what’s happening in both of our scripts to help explain how to get your script to work.

My original script:


enter.onRelease = function() {
    String(input);
    output = parseInt(input, 10);
};

Ok, so basically, when the user clicks the SUBMIT (or enter button), the onRelease method is run. This first converts the inputted number (variable “input” in this case) into a string, which is required for the parseInt function. Next, you set the variable “output” to the result of the parseInt function, which converts the inputted string to a decimal integer. Keep in mind that all of this happens only AFTER the user has clicked the submit/enter button.

Here’s your script:


String(input);
test = parseInt(input, 10);
enter.onRelease = function() {
    output = test
};

First, the variable “input” is converted into a string, but the user hasn’t clicked submit/enter yet, so “input” contains either random gibberish, or is undefined. Next the variable “test” is set to the result of the parseInt function. Well, the parseInt function return “NaN” if it runs into an error (I think NaN stands for Not a Number). Then, when the user clicks the button, the onRelease method is run which sets the “output” variable to NaN, which is probably what you see, right?

So you can see that the only difference between our scripts is when they are run. The “input” variable has to be converted to a string AFTER the user has clicked submit. Then the string has to be parsed after that.

You can, of course, rename any of the variables “input” and “output” to whatever you want, just keep in mind when specific events occur. In this case, everything has to occur AFTER the user clicks the button. I’m not 100% sure what you’re trying to achieve, so if you can describe it maybe I can help further.

-Al