SRL Archive - Research & Development Lounge

Couple Functions I've made [IsMultiple, ReverseString, FormatNumber]


deathbyvirus

2008-08-06 17:10:28

Hey guys, well these are a couple functions I've created for making my progress report and figure they might be useful to some, IsMultiple and ReverseString are required if you want to use FormatNumber because it calls them, otherwise I probably wouldn't have put them in here because their fairly simple and basic. Anyways here they are (remember, if you use these, credit me):


{*******************************************************************************
function IsMultiple(Number, Multiple: integer): boolean;
by: DeathByVirus
Description: Checks if "Number" is a multiple of "Multiple". (eg. IsMultiple(45, 5)
will return true, because 45 is a multiple of 5, as with IsMultiple(6, 2), which will return as true)
*******************************************************************************}

function IsMultiple(Number, Multiple: integer): boolean;
begin
repeat
Number := Number - Multiple;
until (Number = 0) or (Number < 0)

if Number = 0 then
Result := true
else
Result := false;
end;

{*******************************************************************************
function ReverseString(S: string): string;
by: DeathByVirus
Description: Will return the reverse of "S" (eg. ReverseString('Hello') will return 'olleH')
*******************************************************************************}

function ReverseString(S: String): String;
var
i:integer;
begin
for i := 0 to Length(S) - 1 do begin
Result := Result + S[Length(S) - i];
end;
end;

{*******************************************************************************
function FormatNumber(Num: integer): string;
by: DeathByVirus
Description: Will return a number with the appropriate separators (eg. FormatNumber(1820486) will return as '1,820,486')
*******************************************************************************}

function FormatNumber (Num: integer): String;
var
i: integer;
Backwards: String;
Formated: String;

begin
if Length(IntToStr(Num)) <= 4 then Result := IntToStr(Num)
else begin
Backwards := ReverseString(IntToStr(Num));

for i := 1 to Length(Backwards) do begin
Formated := Formated + Backwards[i]

if (IsMultiple(i, 3)) and (i <> Length(Backwards)) then
Formated := Formated + ',';
end;

Result := ReverseString(Formated);

end;
end;

Starblaster100

2008-08-06 18:14:51

Hey, they are good functions

These are two of mine, I thought I would just post them so you can see different (and maybe even more efficient!) ways of scripting.

Enjoy!

[php]
function ReverseString(S: string): string; stdcall;
var
i: Integer;
begin
for i := Length(S) downto 1 do
Result := Result + Copy(S, i, 1);
end;

function IsMultiple(No, Multiple: Integer): Boolean;
begin
Result := (No mod Multiple = 0)
end;
[/php]

deathbyvirus

2008-08-06 18:35:00

there it is! lol i was seaching for a way to do counting down for loops but after searching the forums couldn't find it, and as for the IsMultiple one, i don't really know the mod function, i learned a bit of it when i was learning Turing (another basic line programing language, pascal based) but the teacher i had in school really had no understanding of programing and was only teaching it cuz there were no other teachers that could do it, so i don't really understand it, wanna help me out with it??? thanks

Method

2008-08-06 18:44:39

The mod function gives you the remainder of one number divided by another. You can check out the Wikipedia article for more information.

Cool functions, by the way. :)

deathbyvirus

2008-08-06 18:56:20

i sorta get it now, wiki is never good for learning mathematical functions and other stuff, it's got all these crazy symbols that i'm to lazy to read up on what they mean haha