1 2 Previous Next 48 Replies Latest reply on Aug 5, 2010 2:56 PM by kglad

to 4 decimal points

Ok i'm lost, even though I've asked this before.

I've got my file here http://home.exetel.com.au/twistedpancreas/images/test1.fla , calculating an equation.

But once again I need the answer to be to 4 decimal points (ie like an answer of 0.0393), but for the life of me I can't get it to work.

I tried *100 / 100 but i'm just confusing myself.

Sorry for my lack of knowledge, but i appreciate the help.
• 1. Re: to 4 decimal points
actually ignore this post, i just worked it out

is there anyway to delete posts?
• 2. Re: to 4 decimal points
i don't think so. at least, i've never figured it out if there is a way.
• 3. Re: to 4 decimal points
actually i will continue on with this question i had.

see http://home.exetel.com.au/twistedpancreas/images/test2.fla now

the problem i have now is that whenever a number comes up with a "0" at the end it drops it off. I've asked this before (see http://www.adobe.com/cfusion/webforums/forum/messageview.cfm?catid=288&threadid=1213289 ) but for some reason arunbe's code is throwing in double decimal points now.

What am i doing wrong?
• 4. Re: to 4 decimal points
i've also tried

var my_str:String = input3.text;
trace(my_str.length); // output: 6

if (my_str != 6)

input3.text = input3.text + "0";

else
{
input3.text = input3.text;
}

but this just adds on 0's no matter what, hmm i'll keep on soldiering on unless you guys know a better way.
• 5. Re: to 4 decimal points
if you want to see DecimalPlaces number of digits after a decimal point from roundToDecimalPlace() use:

• 6. Re: to 4 decimal points
woah that works, but i'm blown away as to how :)

any chance of a small description or sould i just be happy with what i got off you kglad

thanks bigtime!
• 7. Re: to 4 decimal points
i put comments that explain the various branch points in the code. but expanding on those comments:

1. flash inserts a leading 0 and decimal for any number less than one. so, in that branch the number (converted to a string) should have lenghth 2+DecimalPlaces.

2. if the number is greater than 1, it may or may not have a decimal. so, in that branch both possiblities are handled.

2a. it has a decimal. then you just need to ensure that there are DecimalPlaces number of digits after the decimal.

2b. it has no decimal. so append a decimal followed by DecimalPlaces number of zeros.
• 8. to 4 decimal points

last thing,

i'm now trying to get input3 as a stored variable, and then the user (who will have the equation) has to type in the final answer

but for some reason i must be using the wrong syntax cause im getting a NAN result, see http://home.exetel.com.au/twistedpancreas/images/test5.fla

• 9. Re: to 4 decimal points
roundToDecimalPlace() returns a string, not a number. and you don't want to convert it to a number or you'll lose terminal zeros and possibly the decimal point.
• 10. Re: to 4 decimal points
sorry im a bit lost again,

so do i need to make it a string via

var input3:String = roundToDecimalPlace((Number(input1.text)/Number(divisNum))/Number(input2.text),4);
trace(input3)

or have i got the wrong "if" code for the button, ie should the comparrison of

if (input4.text == input3)

be something else
• 11. Re: to 4 decimal points
roundToDecimalPlace() returns a string. you don't need to do anything to that. but if you convert that string to a number, then you'll lose the formatting that you want.

so, use a numeric variable for your calculations and roundToDecimalPlace() to "numbers" that you want to display with your formatting (ie, some number of digits after a decimal point).
• 12. Re: to 4 decimal points
using a margin of errors

eg

if ((Number(input4.text) - Number(input3)) < 0.0001) ..

to compare to 4 decimal places
--
Jeckyl

• 13. Re: to 4 decimal points
ok kglad should i be going,

var input3:Number = roundToDecimalPlace((Number(input1.text)/Number(divisNum))/Number(input2.text),4);
trace(input3)

because i'm still getting

NaN
NaN.10000

it seems once i take

input3 = roundToDecimalPlace((Number(input1.text)/Number(divisNum))/Number(input2.text),4);
trace(input3)

code out of the onCalculate button it ceases to work (or maybe my feeble brain just isn't catching up with you gurus)

and I would try your code Jeckyl but until input3 gives me a number, then i still won't get a numeric result

sorry for my lack of coding experience, but thanks for your help thus far
• 14. Re: to 4 decimal points
Perhaps I'm not following the intricacies of your example,

but if you just want to compare two numbers to 4 decimal places (to see if
an answer is correct), then you don't need to do all the stuffing about
you're doing.

If you calculate an answer 'a', and user has entered guess 'g', then you
only need to say

if (Math.abs(a-g) < 0.0001)

to test if the answer is correct to 4 decimal places.

If the values in a or g are strings, rather than numbers, then you need
Number(a) and Number(g) in the above condition.

If you want to display the correct answer to 4 decimal places, use the
rounding code that kglad gave you to give you a string with 4 decimal places
in it.
--
Jeckyl

• 15. Re: to 4 decimal points
ok, jeckyl has a good point and perhaps i answered a question that wasn't what you should be asking.

are you trying to display a "number" that has 4 digits beyond the decimal point (so a user always sees something like 5.1234 or 2.1100) or are you trying to do calculations that are accurate to within 5/100000?

or are you trying to do both/
• 16. Re: to 4 decimal points
Sorry let me clarify, it's for a chemistry equation which will be used in a Flash prelab (ie before the students do the real experiment).

without explaining the whole prelab, the students in the end find a mass (which is a random number within the range of 0.140 to 0.180, and to 3 decimal places, eg 0.142), a difference in volume of (which is also a random number within a range of 0.0205-0.0207, and to 4 decimal places, eg 0.0205) and they use these variables in this equation:

(mass / 176.1)/difference in volume = answer (to 4 decimal places, eg 0.0393)

keeping in mind the client always wants a "0" at the end of an answer which might be 0.0390

I hope that helps and sorry for the confusion.
• 17. Re: to 4 decimal points
then you'll need to use both the actual number for all calculations and then for a final display use roundToDecimalPlace(). as jeckyl pointed out, you can use the condition

to determine if a student's answer is accurate to within 3 places past the decimal and

to determine if a student's answer is within 4 places past the decimal.
• 18. to 4 decimal points
thx so far guys

is this getting closer to being right? i still don't think input3 is being outputted as a number, and i think i've stuffed up the Math.abs(input3-input4.text)<.0001

this.input1.text = setVal();
function setVal() {
val = Math.round((0.140+Math.random()*0.040)*1000);
val = "0."+val;
return (val);
}

this.input2.text = setVal1();
function setVal1() {
val1 = Math.round((0.205+Math.random()*0.002)*1000);
val1 = "0.0"+val1;
return (val1);
}

this.input3.text = roundToDecimalPlace((Number(input1.text)/Number(divisNum))/Number(input2.text),4);
trace(input3)

function roundToDecimalPlace(NumbertoConvert, DecimalPlaces) {
var input3 = Math.pow(10, DecimalPlaces);
var numericVal = Math.round(NumbertoConvert*input3)/input3;
trace(numericVal);
var stringVal = ""+numericVal;
if (numericVal<1) {
// length should be 2+DecimalPlaces
while (stringVal.length<2+DecimalPlaces) {
stringVal = stringVal+"0";
}
} else {
// should have four digits after the decimal, if there is a decimal
decimalPos = stringVal.indexOf(".");
if (decimalPos>-1) {
// has a decimal
while (stringVal.charAt(decimalPos+DecimalPlaces) == "") {
stringVal = stringVal+"0";
}
} else {
// no decimal
stringVal = stringVal+"."+input3;
}
}
return stringVal;
}

var divisNum:Number = 176.1;

onCalculate.onPress = function()
{

if (input4.text == input3) & Math.abs(input3-input4.text)<.0001

{
}

else
{
}

}
• 19. Re: to 4 decimal points
correct the code in the first few lines of onCalculate.onPress and retest:

• 20. Re: to 4 decimal points
sorry from my

trace(input3)

i'm still getting a result of

NaN
undefined

which will give me NaN-input4.text won't it?
• 21. Re: to 4 decimal points
is input3 a string? do you need to convert it to a number?

you can use trace(typeof(input3)) to determine object-type.
• 22. to 4 decimal points
ooops input3 should be as:

this.input3 = roundToDecimalPlace((Number(input1.text)/Number(divisNum))/Number(input2.text),4);
trace(input3)

does that automatically make it a string?

because i tried

convertToNumber = Number(input3);
trace(convertToNumber)

i also tried

(typeof(input3));

(typeof(convertToNumber));

and was still getting NAN, or does (typeof(input3)) not appear in the Output window?

also, now, when you click the calculate button the answer is Correct no matter what the answer.
• 24. Re: to 4 decimal points
yeah sorry i was going to do that but our network here was mucking up

ok here it is http://home.exetel.com.au/twistedpancreas/images/test7.fla
• 25. to 4 decimal points
actually i've updated my file to resemble how the setup will work

see http://home.exetel.com.au/twistedpancreas/images/test8.fla

i now have input3 as iodine and it's now giving me an answer but i think it's stuck on trying to round the number down to 3 significant figures (ie 0.0210), thus the answer will always be incorrect at the moment

any help would be most appreciated
• 26. Re: to 4 decimal points
try:

• 27. Re: to 4 decimal points

it's all now making a little more sense, although i've still got to get my head around some of the code (but big thanks to all who helped out)

my latest file is now http://home.exetel.com.au/twistedpancreas/images/test9.fla

got one small additional question (and maybe it should be a new topic), but is there anyway for the first check button to check for all numbers within the random range of numbers and return them as the correct answer if typed into titration.text (ie titrationValue = any result within the range of 0.02050 to 0.02080)?

i hope that makes sense
• 28. Re: to 4 decimal points
you're welcome.

you can check:

• 29. Re: to 4 decimal points
you are a living legend kglad and you get my favourite person of the day :D

thanks a million!!!

my knowledge is slowly building, and a lot of it is due to this forum, thx all!
• 30. Re: to 4 decimal points
you're welcome.
• 31. to 4 decimal points
sorry to bring this one up again, but my client is now wanting this code to end up with no decimal point, ie if the answer was 446.5, then they'd want the answer as 447, and i'm a bit frazzled as to what to do, here is my code at the moment...

• 32. Re: to 4 decimal points
> sorry to bring this one up again, but my client is now wanting this code
> to end
> up with no decimal point, ie if the answer was 446.5, then they'd want the
> answer as 447, here is my code at the moment...

That is SO much simpler .. just use Math.round

var multiNum:Number = 723699;
this.iodine = (Number(molInput.text)*Number(titrationInput.text))*multiNum;
this.iodine = Math.round(this.iodine);
trace(this.iodine)
this.titrationDisplay.text = this.iodine;
--
Jeckyl

• 33. Re: to 4 decimal points
um i'm getting NaN when molInput.text = 0.0391 and titrationInput.text = 0.01630
• 34. Re: to 4 decimal points
isn't iodine being defined twice there?

shouldn't it be something like

• 35. Re: to 4 decimal points
> isn't iodine being defined twice there?

No its fine

• 36. Re: to 4 decimal points
> um i'm getting NaN when molInput.text = 0.0391 and titrationInput.text =
> 0.01630

Then there is something going on you're not telling us, as the script will
work with those inputs .. are you sure there.s not other characters in the
strings?
--
Jeckyl

• 37. Re: to 4 decimal points
yeah could be (knowing me)

here's my file: http://home.exetel.com.au/twistedpancreas/roundingNumbersTest.fla

it's got me stumped!