AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Last Digit from a Integer (https://forums.alliedmods.net/showthread.php?t=121031)

meTaLiCroSS 03-10-2010 20:24

Last Digit from a Integer
 
How i can get the last digit from a integer?

Some examples:

3451
789
20
1

(retard example, ftw)

Thanks in advance.

wrecked_ 03-10-2010 20:35

Re: Last Digit from a Integer
 
I assume it'd be something like this.
PHP Code:

new 26

new number[] = num_to_str)

new 
lastdigit str_to_numnumbersizeof number ] )
// or if you want it as a string
new lastdigit numbersizeof number 


AntiBots 03-10-2010 20:40

Re: Last Digit from a Integer
 
Quote:

Originally Posted by wrecked_ (Post 1114057)
I assume it'd be something like this.
Code:
new i = 26 new number[] = num_to_str( i ) new lastdigit = number[ sizeof number - 1 ]

Suck Method.

PHP Code:

stock get_last_num( const int )
{
    return 
int 10 * (int 10);


I think that is correct. Just Test. I don't!!! :wink:



EDIT: Exolent method rocks!!

wrecked_ 03-10-2010 20:45

Re: Last Digit from a Integer
 
Quote:

Originally Posted by AntiBots (Post 1114062)
Suck Method.

PHP Code:

stock get_last_num( const int )
{
    return 
int 10 * (int 10);


I think that is correct. Just Test. I don't!!! :wink:

What? That makes no sense.

26 - 10 * (26 / 10 )
non-PEMDAS: 41.6
PEMDAS: 0

Exolent[jNr] 03-10-2010 20:45

Re: Last Digit from a Integer
 
You guys are either retarded or didn't think.

Code:
new iNumber = 123456; new iLastDigit = iNumber % 10; // iLastDigit = 6

Seta00 03-10-2010 21:09

Re: Last Digit from a Integer
 
Quote:

Originally Posted by Exolent[jNr] (Post 1114067)
You guys are either retarded or didn't think.

Code:
new iNumber = 123456; new iLastDigit = iNumber % 10; // iLastDigit = 6

EPIC PWNAGE

Programming require math skills... Actually, programming is just math hiding behind identifiers and operators.

wrecked_ 03-10-2010 21:13

Re: Last Digit from a Integer
 
What exactly does the modulo operator return? I'm aware that it has something to do with the remainder, but that operation makes no sense at all to me. I would've thought it'd return 0, since the remainder of 123456 / 10 is 0.

YamiKaitou 03-10-2010 21:22

Re: Last Digit from a Integer
 
Quote:

Originally Posted by wrecked_ (Post 1114101)
What exactly does the modulo operator return? I'm aware that it has something to do with the remainder, but that operation makes no sense at all to me. I would've thought it'd return 0, since the remainder of 123456 / 10 is 0.

You fail at math if you think that. % returns the remainder of the division problem. 6 / 10 leaves you with a remainder of 6...

wrecked_ 03-10-2010 21:26

Re: Last Digit from a Integer
 
Quote:

Originally Posted by YamiKaitou (Post 1114107)
You fail at math if you think that. % returns the remainder of the division problem. 6 / 10 leaves you with a remainder of 6...

Oh, come to think of it, that was quite a dumb statement.

:evil: My bad.

AntiBots 03-10-2010 21:34

Re: Last Digit from a Integer
 
% work like this:

I want to do: 20 % 8;

PHP Code:

Well:
0    1    2    3    4    5    6    7: ( 0 to X % (this-1) )

0    1    2    3    4    5    6    7
8    9   10   11   12   13   14   15
16  17   18   19   20 

So 20 % 8: 4

Algo you can think:

20 / 8 = 2
20 % 8 = 4.

4 / 8 = 0.5

So: 8 * 2 + 4 = 20;

Just a math lesson.


All times are GMT -4. The time now is 19:49.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.