Game Development Community

How do you get the last two didgets of a number?

by Nic Biondi · in General Discussion · 01/28/2004 (4:14 pm) · 7 replies

How do you get the last two didgets of a number? If I have a number x=390 how do I get a y so that y-90 for an arbitrary case?

I know that if you multiply x by .01 you can get the desired didgets into a decimal, but I don't know if that helps.
ie: 390*.01=3.90

#1
01/28/2004 (4:15 pm)
Sorry, by "y-90" I meant to say "y=90".
#2
01/28/2004 (4:20 pm)
Function lastdigits(%num)
{
%lastdigits = %num - (floor(%num/100) * 100);
return %lastdigits;
}
#3
01/28/2004 (4:35 pm)
Your a genuis harold, nice to see you again, and thanx for the tip
#4
02/03/2004 (6:20 pm)
Modulus (remainder) should work too:

i = num % 100; //(returns number 0 - 100)

in both C++ and torque script
#5
02/03/2004 (6:58 pm)
Yep, that would be much simpler.

Hmm... thinking about it... this would be a much better function:

function lastdigits(%num, %places)
{
	if(%places > 0)
		%pow = %places;
	else
		%pow = strlen(%num)-1;
	
	%result = %num % mPow(10,%pow);

	return %result;
}

This will either give you the number of digits you specify or everything except the first digit.
#6
02/04/2004 (1:20 am)
Im using the lastDidget function that you first posted Harold, and it seems to work fine. Don't need anything too much fancier. Neat trick though Pascal
#7
12/15/2004 (5:45 am)
I guess I misunderstood the question. I was looking find if a number was a decimal.

I modified Harold's original Function:


function lastdigits(%num)
{
%lastdigits = %num - (mfloor(%num));
return %lastdigits;
}


ie.
echo(lastdigits(10.77));

returns 0.77

echo(lastdigits(-10.77));

returns 0.23

if return value > 0 && return value < 1


It's a decimal.