12 Replies Latest reply on Aug 2, 2006 10:51 PM by Craig Grummitt

# Decimal point reduction

here's my file where you can add, subtract, multiply and divide 2 numbers all at once.

http://home.exetel.com.au/twistedpancreas/images/calculations.fla

problem is, sometimes the answers are to 14 decimal points.

how can i change this so that the answers will only be to say 2 decimal points?

• ###### 1. Re: Decimal point reduction
Have a look here:

--

Dan Mode
*Flash Helps* http://www.smithmediafusion.com/blog/?cat=11

"twistedpancreas" <webforumsuser@macromedia.com> wrote in message
news:eapaa3\$mg1\$1@forums.macromedia.com...
> here's my file where you can add, subtract, multiply and divide 2 numbers
> all
> at once.
>
> http://home.exetel.com.au/twistedpancreas/images/calculations.fla
>
> problem is, sometimes the answers are to 14 decimal points.
>
> how can i change this so that the answers will only be to say 2 decimal
> points?
>
>

• ###### 2. Re: Decimal point reduction
Here's a function to handle rounding to decimal places:
• ###### 3. Re: Decimal point reduction
Thanks guys,

I see what both of you are saying, but you're options need a number typed into the code before executing.

What i have in mine in the number being generated after you've executed the swf file and divided 2 numbers giving a result in the division.text

so what i'd like to have is the division.text result in the code instead of a hard coded number, like this:

value= int((division.text)*100)/100;

but that doesnt work

do i need to set up a variable or something?

thanks for you're help so far!
• ###### 4. Re: Decimal point reduction
division.text = (Number(input1.text)/Number(input2.text));

add my function and then change the above line to:
division.text = roundToDecimalPlace(Number(input1.text)/Number(input2.text),2);
• ###### 5. Re: Decimal point reduction
Thanks Craig!

That's awesome!

So where can I find a description that explains you're code, cause it works, but I'd like to know how it works (ie if I ever wanted to have the decimals to 3 points, or more, I'd like to know how to do that)
• ###### 6. Re: Decimal point reduction
ok how to use the function :
roundToDecimalPlace(NumbertoConvert, DecimalPlaces)

Parameters:
NumbertoConvert - the number you want to convert.
DecimalPlaces - the number of decimal places

Example:
If you wanted to round pi to 7 decimal places and place the answer in the variable piRounded, you would write:

var piRounded=roundToDecimalPlace(Math.PI,7)
• ###### 7. Re: Decimal point reduction
ok i'm a bit confused now

so NumbertoConvert and DecimalPlaces need actual numbers?

sorry i thought they were code that carried out some action

here's my updated file

http://home.exetel.com.au/twistedpancreas/images/calculations1.fla

should the random number replace the NumbertoConvert text?
• ###### 8. Re: Decimal point reduction
i think maybe you're a bit confused about how functions work.

return(firstNumber+secondNumber);
}

this function will add together two numbers. if you wanted to add 1+2 and put the answer into a variable called sum you would do this:

also, the code i gave you before with the function:

//sample function call
trace(roundToDecimalPlace(Math.PI,2));

didn't have to go into your application. It was merely an example of how to use the function.

apart from those extra couple of lines, your application seems okay to me - it's working how you want it to isn't it?
• ###### 9. Re: Decimal point reduction
ah ok

so to get

division.text = roundToDecimalPlace(Number(input1.text)/Number(input2.text),2);

to 7 decimal points, i'd have to have

division.text = roundToDecimalPlace(Number(input1.text)/Number(input2.text),7);

is that right?

yeah, i'm in the process of looking into getting myself a good book on Actionscripting, since i'm more of a designer than a programmer (thus my problem understanding functions, but thatnks for you're explaination, it was very helpful)

is it alright to ask on here for any book recommendations?
• ###### 10. Re: Decimal point reduction
yes that's right - you're getting there!

book recommendations - go for it - i'll have to invite others to answer that question though as i mainly just use adobe documentation.
• ###### 11. Re: Decimal point reduction
Thanks all for you're help, my Actionscripting knowledge is slowly growing, thanks to guys like yourselves.

Cheers

(PS I'll start a new topic with book recommendations)
• ###### 12. Re: Decimal point reduction
no worries - good luck!