22 Replies Latest reply on Mar 24, 2008 2:02 PM by Mr.Wallace

# COMPLEX MATH

Ok, let me try and explain this intelligently, LOL...

So, I am trying to create a math equation with Flash. I first created it in Excel and it works great! The problem is getting Flash to do the same type of thing.

Basically I am trying to create something like this:

Text One: [ input text box ]

[Calculate Button]

Text Two: [ empty text box ]
Text Three: [ empty text box ]

So, I want to be able to input a dollar amount, for example \$20.83, into the input text box then press the Calculate Button that will display numerical amounts in text box 2 and 3. That's easy, I know...here's the hard part...

If the dollar amount entered into the input field in \$20.83, I need text box 2 to display the rounded up value of that. Meaning, if I enter \$20.83 into the input field and press Calculate, text box 2 should display \$21.00 (rounded up to the nearest dollar). If I enter \$21.35, for example, then text box 2 should display \$22.00 and so on. Basically rounding the dollar amount up to the nearest dollar amount.

Figured that out have you? Then I got one more thing, LOL!

At the same time as the above happens, I need text box 3 to display the total of text box 2 minus input text one. For example:

Input text: \$21.34
[CALCULATE]
Text Box 2: \$22.00
Text Box 3: \$0.66

So, after all that rambling...is this even possible? Anybody? PLEEEEEEASE help me out, LOL!!!

C.
• ###### 1. COMPLEX MATH
Okay, I've got this working for me:
• ###### 2. Re: COMPLEX MATH
This is close to what you want I think. The extra rounding seemed to be necessary, it will round to the nearest cent.

input1 is the name of the input text field
textfield2
textfield3
are the display textfields (dynamic)
and mybutton is your button to press.
• ###### 3. COMPLEX MATH
WOW!

Ok, that was AMAZING!!! Thank you SO much, I appreciate that the efforts!

One question...when it does the subtraction it's giving me exact solutions, meaning, if the answer is \$0.46, it gives me 0.46000000001 or whatever. Is that adjustable or no?

Even if it isn't...that was awesome. THANK YOU so much!!!
• ###### 4. Re: COMPLEX MATH
Mr. Wallace,

That's the problem my solution was running into. The code I posted above fixes that by:
1) Multiplying the long fraction (.466666666) by 100 giving 46.66666666
2) Applying the Math.ceil function to that number returning 46
3) Dividing that by 100 returning .46
• ###### 5. Re: COMPLEX MATH
dex...YOU ROCK!!!

That totally worked, THANK YOU SOOOOO MUCH!

I am very, VERY appreciative!
• ###### 6. Re: COMPLEX MATH
Hey GWD,

THANKS A MILLION!!!
• ###### 7. COMPLEX MATH
Is it possible to do the same thing but make the 'textbox2' amounts round up to the next \$5 amount or no?

Example:

Input Text: 25.77

[CALCULATE]

Text Field2: 30.00

Text Field3: 4.23

Possible or no?

PS. MAN, those scripts were great! LOL!!!
• ###### 8. Re: COMPLEX MATH
WHOA! WHOA! Got a problem here dex.

When I enter amounts ending in 1, for example 34.61, the rounded up number is correct but the subtraction is not. In the subtraction field it's saying that the answer is 0.4, but the answer should be 0.39.

Any ideas?
• ###### 9. COMPLEX MATH
GWD,

Your code seems to do the trick as far as solving my above point of the amounts ending in 1 not adding up. One thing though...how can I get the \$ symbol out of the input field once I press CALCULATE?

It was cool that you did that but I can't use that in my application.

:)

Also, if you had any ideas about how to round the numbers up to the next \$5 amount, that you be cool too!

Example:

Input Text 1: 32.25
[CALCULATE]
Text Field 2: 35.00
Text Field 3: 2.75
• ###### 10. Re: COMPLEX MATH
I was away for a bit there. The line:
input1.text="\$"+val
just delete it or change it to
input1.text=val
to force the input to 2 decimal places (max)

It is possible to force formatting to be 2.10 instead of 2.1 for example, but that requires (a small amount of) extra code.

for rounding to the nearest 5 you could use a function like this:

function roundTo5(val:Number){
return (Math.round(val*.2)*5)
}

• ###### 11. Re: COMPLEX MATH
Or to always round up to the next 5, like this:

function roundTo5(val:Number){
return (Math.ceil(val*.2)*5)
}
• ###### 12. Re: COMPLEX MATH
So I would include that in the previous code that you sent me?
• ###### 13. Re: COMPLEX MATH
Yes, like this:
• ###### 14. Re: COMPLEX MATH
Wow...very, VERY cool!

Thank you again.

So what is it you're changing/adjusting when you want to round to the next \$5? I'm assuming that you can do that for any value, yes? If I wanted to round to the next \$2 increment or \$10 increment I could just change all the 5's in the code to 2 or 10?
• ###### 15. Re: COMPLEX MATH
That's just a way to round to multiples (or round up to multiples in your case).

Its probably easier to understand like this:

var val2=roundToWhatever(10,val);

function roundToWhatever(whatever:Number,val:Number){
return (Math.ceil(val/whatever)*whatever)
}

That's the example for 10. So if you put 6 in there instead of 10, it will round up to the next multiple of 6... 6,12,18 etc.
• ###### 16. Re: COMPLEX MATH
Man, thank you so much for all your help! This was greatly appreciated and very informative.

THANKS!!!
• ###### 17. Re: COMPLEX MATH
So, what's wrong with this code? I'm trying to get it to round to the nearest 10th:

• ###### 18. Re: COMPLEX MATH
Sorry, I mean the nearest \$10, not 10th.
• ###### 19. Re: COMPLEX MATH
You need to change the value that the function returns, changing its name alone will do nothing different. Or you can use the generic rounding up to multiples function I posted before.

function roundTo10(val:Number){
return (Math.ceil(val*.1)*10)
}
• ###### 20. Re: COMPLEX MATH
So it's the RETURN section I'm screwing up. My question is, how are you coming up with the numbers you're using. For the \$10 increments you used:

function roundTo10(val:Number){
return (Math.ceil(val*.1)*10)
}

but in the \$5 example you used:

function roundTo5(val:Number){
return (Math.ceil(val*.2)*5)
}

Maybe I'm just not the greatest math person or something?! It seems like it should be easy. What if I wanted to round the nearest \$2 for example...which two numbers would I use then? Obviously the last number would be *2 but what would the decimal be?

I apologize if I'm getting annoying, but I just want to make sure I understand completely.
• ###### 21. Re: COMPLEX MATH
Its OK its not annoying, lol... just a failure in my communication I guess. I thought the function I posted earlier would make it clearer (I guess it didn't):

function roundToWhatever(whatever:Number,val:Number){
return (Math.ceil(val/whatever)*whatever)
}

Look at the 'whatever' variable in the above function

for 5:
its val/5 which is the same as val*.2 then its rounded (up) and multiplied by 5 again

for 10:
its val/10 which is the same as val*.1 then its rounded (up) and multiplied by 10 again

for 2:
its val/2 which is the same as val*.5 then its rounded (up) and multiplied by 2 again

using the function above, you can use one function and pass the rounding argument as well as the value to be rounded.
trace( roundToWhatever(2,3.5)) // shows 4 (3.5 rounded up to the next multiple of 2)
trace( roundToWhatever(10,3.5)) // shows 10 (3.5 rounded up to the next multiple of 10)
trace( roundToWhatever(5,3.5)) // shows 5 (3.5 rounded up to the next multiple of 5)

• ###### 22. Re: COMPLEX MATH
Ok...I think I can wrap my head around, LOL!

Thanks a lot for ALL your help...this was greatly appreciated!!!