Finding number of significant digits

Hello, I am creating an application in Adobe Flash Actionscript 3, and I need a function that can inspect and return the number of significant digits in a floating point number.  I have tried comparing results of String(myNum) and myNum.toPrecision(SigDigsRequired), however, this function breaks down with numbers like 200, where

trace("200",  Number(200).toPrecision(2),  Number(200).toExponential(2));

gives me this:
200   2.0e+2   2.00e+2

What I really need is a function like getNumberOfSigDigs(number)

Any Ideas?
kcvsAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

dhsindyRetired considering supplemental income.Commented:
Significant digits depends on where the number came from (accuracy of the measurement, etc.).  I don't think you can have a computer function that can determine the accuracy of the input.  You would have to pre-define what the inputs represent.
0
rascalpantsCommented:

this seems like a good use for a Regular Expression, which i suck at, or just a string comparison with conditionals...

based on the rules here...

1. All non-zero digits are significant.
2. In a number without a decimal point, only zeros BETWEEN non-zero digits are significant (unless a bar indicates the last significant digit--see below).
3. In a number with a decimal point, all zeros to the right of the first non-zero digit are significant.

from....  http://en.wikipedia.org/wiki/Significant_digit


I think there is a pretty good chance that you can come up with an algorithm for it.

maybe a conditional like this...


basically, you just pass a String into the function... if you try to pass a Number, it will take a number like 100.0 and turn it to 100


does this work for you?


rp / ZA

function checkForSig( theNum:String ):int
{
	var sigNums:int;
	if( theNum.indexOf(".") > -1 ) sigNums = theNum.split(".").join("").length;
	else sigNums = theNum.split("0").join("").length;
 
	return sigNums;
}
 
trace( checkForSig( "100.01" ) );  // give me 5

Open in new window

0
rascalpantsCommented:

actually, the last one had a fault...

try this one...  let me know if you can find a number that is not caught...


rp / ZA



// AS 3.0
 
function checkForSig( theNum:String ):int
{
	var sigNums:int;
	if( theNum.indexOf(".") > -1 )
	{
		sigNums = theNum.split(".").join("").length;
	}
	else if( theNum.indexOf("0") > -1 )
	{
		var arr:Array = theNum.split("");
		var len:int = arr.length;
		sigNums = arr[ len - 1 ] == "0" ? theNum.split("0").join("").length : theNum.length;
	}
	else
	{
		sigNums = theNum.length;
	}
	
	return sigNums;
}
 
trace( checkForSig( "10.50" ) );

Open in new window

0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

kcvsAuthor Commented:
Hi rascalpants, I found that your algorithm did not work for 0.0000010 (which should be 2):

trace( checkForSig( "0.0000010" ) );
//outputs 8
0
rascalpantsCommented:


and one more update to fix the occurance of 0.00012 as having 6 instead of the correct value of 2...


hope this works for you...  at least it was a fun challenge...


rp / ZA

// AS 3.0
 
function checkForSig( theNum:String ):int
{
	var sigNums:int;
	if( theNum.indexOf(".") > -1 )
	{
		var decArr:Array = theNum.split(".");
		sigNums = decArr[ 0 ] == "0" ? decArr.join("").split("0").join("").length : decArr.join("").length;
	}
	else if( theNum.indexOf("0") > -1 )
	{
		var arr:Array = theNum.split("");
		var len:int = arr.length;
		sigNums = arr[ len - 1 ] == "0" ? theNum.split("0").join("").length : theNum.length;
	}
	else
	{
		sigNums = theNum.length;
	}
	
	return sigNums;
}
 
trace( checkForSig( "120.00" ) );

Open in new window

0
rascalpantsCommented:
you where too quick for me  ;)


rp / ZA
0
rascalpantsCommented:
hold on one more second...

:)

rp / ZA
0
rascalpantsCommented:


okay... I think I got them all this time... had to use some fancy foot work with a number string type thing...

:)


rp / ZA

// AS 3.0
 
function checkForSig( theNum:String ):int {
	
	var sigNums:int;
	if ( theNum.indexOf(".") > -1 )
	{
		var decArr:Array = theNum.split(".");
		sigNums = decArr[ 0 ] == "0" ? String( Number(decArr[1]) ).length : decArr.join("").length;
	}
	else if ( theNum.indexOf("0") > -1 )
	{
		sigNums = String( Number( String( "." + Number( theNum ) ) ) ).split(".")[1].length;
	}
	else
	{
		sigNums = theNum.length;
	}
	return sigNums;
}
 
trace( checkForSig( "1" ) + " = 1" );
trace( checkForSig( "300" ) + " = 1" );
trace( checkForSig( "0.0012" ) + " = 2" );
trace( checkForSig( "0.0120" ) + " = 3" );
trace( checkForSig( "10.0120" ) + " = 6" );

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
kcvsAuthor Commented:
Thanks!
0
rascalpantsCommented:
no problem...  I love a challenge  ;)

what exactly can this be used for now?  lol




rp / ZA

0
kcvsAuthor Commented:
I have a graphing component in flash, and in the interest of making a graph that looks nice, I am trying to find a suitable value for the difference between consecutive x-axis labels.  The graphing utility can graph any range of values, and supports zooming and moving around.  This causes some pretty messy numbers to be displayed on the x-axis labels.  This code will help me find the best difference between the consecutive x-labels so the display is intervals are easy to read.

:)
0
rascalpantsCommented:

wow...  my code sounds like childs play compared to what you are putting it into

;)

good luck...

rp / ZA
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Adobe Flash

From novice to tech pro — start learning today.