Skip to main content
Inspiring
November 23, 2006
Question

to 4 decimal points

  • November 23, 2006
  • 37 replies
  • 4828 views
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.
This topic has been closed for replies.

37 replies

kglad
Community Expert
Community Expert
November 28, 2006
is input3 a string? do you need to convert it to a number?

you can use trace(typeof(input3)) to determine object-type.
Inspiring
November 27, 2006
sorry from my

trace(input3)

i'm still getting a result of

NaN
undefined

which will give me NaN-input4.text won't it?
Inspiring
November 26, 2006
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()
{

var Answer:Number = 0;

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

Answer=1;


if(Answer == 1)
{
answer.text = "Correct";
}

else
{
answer.text = "Incorrect";
}

}
kglad
Community Expert
Community Expert
November 27, 2006
correct the code in the first few lines of onCalculate.onPress and retest:

kglad
Community Expert
Community Expert
November 24, 2006
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

Math.abs(actualAnswer-studentAnswer)<.001

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

Math.abs(actualAnswer-studentAnswer)<.0001

to determine if a student's answer is within 4 places past the decimal.
Inspiring
November 24, 2006
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.
kglad
Community Expert
Community Expert
November 24, 2006
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/
Inspiring
November 24, 2006
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


Inspiring
November 24, 2006
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
kglad
Community Expert
Community Expert
November 23, 2006
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).
kglad
Community Expert
Community Expert
November 23, 2006
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.
Inspiring
November 23, 2006
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