/**
* This is a collection of helpers I wrote to make mathematical problems easier to handle.
* It is freely redistributable without limitations as long as this note stays intact.
* © Franz Balthasar Gutsch
*/

// :int [0..9]	returns the specific digit of the given number (the "String." way)
function getDigit(num,exp) {
	var sNum = num.toString();
	sNum = sNum.replace(/,/,'.');			// if someone used a comma instead of a point
	exp = parseInt(exp);
	
	if (isNaN(exp) || isNaN(sNum)) return false;	//error handling
	
	var i = sNum.indexOf('.');
	if (i == -1) i = sNum.length;			// if there is no point at all 
	
	if (exp >= 0) i--;
		
	if (isNaN(parseInt(sNum.charAt(i-exp)))) {
		return 0;
	} else {
		return  sNum.charAt(i-exp);
	}
}

// :int [0..9]	returns the specific digit of the given number (the "Math." way – limited accuracy)
function getDigitMath(num,exp) {
	return (Math.floor(Math.round(num/Math.pow(10,exp) * 1000000)/1000000)% 10);
}
/**
* This is a collection of helpers I wrote to make mathematical problems easier to handle.
* It is freely redistributable without limitations as long as this note stays intact.
* © Franz Balthasar Gutsch
*/

// :int [0..9]	returns the specific digit of the given number (the "String." way)
function getDigit(num,exp) {

	var sNum = num.toString();
	// "," → "."
	sNum = sNum.replace(/,/,'.');
		
	//error handling
	exp = parseInt(exp);
	if (isNaN(exp) || isNaN(sNum)) return false;
	
	var i = sNum.indexOf('.');

	// if there is no point at all
	if (i == -1) i = sNum.length;

	// separate by pre and after point	
	if (exp >= 0) i--;
	
	// if its a "-" or nothing at all
	if (isNaN(parseInt(sNum.charAt(i-exp)))) {
		return 0;
	} else {
		// do it!
		return  sNum.charAt(i-exp);
	}
}

// :int [0..9]	returns the specific digit of the given number (the "Math." way – limited accuracy)
function getDigitMath(num,exp) {
	return (Math.floor(Math.round(num/Math.pow(10,exp) * 1000000)/1000000)% 10);
}
