# Why is this basic calculation not working?

**URL:** <https://forum.kirupa.com/t/why-is-this-basic-calculation-not-working/9588>\
**Category:** flash\
**Created:** [December 14, 2002, 10:39am UTC](https://forum.kirupa.com/t/why-is-this-basic-calculation-not-working/9588 "2002-12-14T10:39:16Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![byis](https://avatars.discourse-cdn.com/v4/letter/b/5e9695/32.png) [@byis](https://forum.kirupa.com/u/byis)\
**Post date:** [December 14, 2002, 10:39am UTC](https://forum.kirupa.com/t/why-is-this-basic-calculation-not-working/9588/1 "2002-12-14T10:39:16Z")

</div>

EXAMPLE 1

```auto

a = 98587
b = 0711

c = a - b

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

```

EXAMPLE 2

```auto

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.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [December 14, 2002, 12:39pm UTC](https://forum.kirupa.com/t/why-is-this-basic-calculation-not-working/9588/2 "2002-12-14T12:39:43Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [December 15, 2002, 12:25am UTC](https://forum.kirupa.com/t/why-is-this-basic-calculation-not-working/9588/3 "2002-12-15T00:25:52Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [December 15, 2002, 6:03am UTC](https://forum.kirupa.com/t/why-is-this-basic-calculation-not-working/9588/4 "2002-12-15T06:03:21Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [December 15, 2002, 6:11am UTC](https://forum.kirupa.com/t/why-is-this-basic-calculation-not-working/9588/5 "2002-12-15T06:11:33Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [December 15, 2002, 6:51am UTC](https://forum.kirupa.com/t/why-is-this-basic-calculation-not-working/9588/6 "2002-12-15T06:51:37Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [December 15, 2002, 6:59am UTC](https://forum.kirupa.com/t/why-is-this-basic-calculation-not-working/9588/7 "2002-12-15T06:59:29Z")

</div>

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. :+)

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [December 15, 2002, 8:26am UTC](https://forum.kirupa.com/t/why-is-this-basic-calculation-not-working/9588/8 "2002-12-15T08:26:16Z")

</div>

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.

```auto

check = myString .charAt( 0 ) 

```

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [December 15, 2002, 4:21pm UTC](https://forum.kirupa.com/t/why-is-this-basic-calculation-not-working/9588/9 "2002-12-15T16:21:03Z")

</div>

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!):

```php

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

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [December 15, 2002, 8:53pm UTC](https://forum.kirupa.com/t/why-is-this-basic-calculation-not-working/9588/10 "2002-12-15T20:53:40Z")

</div>

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]](http://www.byis.net/remove2.fla)

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [December 15, 2002, 9:18pm UTC](https://forum.kirupa.com/t/why-is-this-basic-calculation-not-working/9588/11 "2002-12-15T21:18:53Z")

</div>

Heya.

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

```php

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

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [December 15, 2002, 9:31pm UTC](https://forum.kirupa.com/t/why-is-this-basic-calculation-not-working/9588/12 "2002-12-15T21:31:45Z")

</div>

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.

```auto

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

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

```

Thanks for your help!

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [December 15, 2002, 10:03pm UTC](https://forum.kirupa.com/t/why-is-this-basic-calculation-not-working/9588/13 "2002-12-15T22:03:39Z")

</div>

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:

```php

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:

```php

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
