to 4 decimal points
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
is there anyway to delete posts?
Copy link to clipboard
Copied
Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Hi again Kglad!
I have an inquiry regarding integers in a similar piece of code you helped with earlier:
I have a basic counter using a dynamic text box named counterText to show:
var curcount:Number= 3.0;
var increment:Number = 0.1;
var si:Number = setInterval(function ()
{
counterText.text = String(curcount += increment);
}, 200);
This works, except when the number is an integer.
What code do I need in AS 2 to get this to show 1 decimal point to right?
Copy link to clipboard
Copied
check your thread about this.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
any chance of a small description or sould i just be happy with what i got off you kglad
thanks bigtime!
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
using a margin of errors
eg
if ((Number(input4.text) - Number(input3)) < 0.0001) ..
to compare to 4 decimal places
--
Jeckyl
Copy link to clipboard
Copied
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).
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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/
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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";
}
}
Copy link to clipboard
Copied
Copy link to clipboard
Copied
trace(input3)
i'm still getting a result of
NaN
undefined
which will give me NaN-input4.text won't it?
Copy link to clipboard
Copied
you can use trace(typeof(input3)) to determine object-type.
Copy link to clipboard
Copied
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.


-
- 1
- 2