Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

to 4 decimal points

Contributor ,
Nov 22, 2006 Nov 22, 2006
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.
TOPICS
ActionScript
4.4K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Nov 22, 2006 Nov 22, 2006
actually ignore this post, i just worked it out

is there anyway to delete posts?
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 22, 2006 Nov 22, 2006
i don't think so. at least, i've never figured it out if there is a way.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Nov 22, 2006 Nov 22, 2006
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?
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 22, 2006 Nov 22, 2006
if you want to see DecimalPlaces number of digits after a decimal point from roundToDecimalPlace() use:

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Aug 05, 2010 Aug 05, 2010

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?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 05, 2010 Aug 05, 2010
LATEST

check your thread about this.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Nov 22, 2006 Nov 22, 2006
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.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Nov 22, 2006 Nov 22, 2006
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!
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 22, 2006 Nov 22, 2006
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.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Nov 22, 2006 Nov 22, 2006
thanks kglad

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 22, 2006 Nov 22, 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.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Nov 23, 2006 Nov 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
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Nov 23, 2006 Nov 23, 2006
You're probably better comparing the inputted answer with the correct answer
using a margin of errors

eg

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

to compare to 4 decimal places
--
Jeckyl


Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 23, 2006 Nov 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).
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Nov 23, 2006 Nov 23, 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
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Nov 23, 2006 Nov 23, 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


Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 23, 2006 Nov 23, 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/
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Nov 23, 2006 Nov 23, 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.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 23, 2006 Nov 23, 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.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Nov 26, 2006 Nov 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";
}

}
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 26, 2006 Nov 26, 2006
correct the code in the first few lines of onCalculate.onPress and retest:

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Nov 27, 2006 Nov 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?
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 28, 2006 Nov 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.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Nov 28, 2006 Nov 28, 2006
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.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines