Couple Functions I've made [IsMultiple, ReverseString, FormatNumber]
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):
Code:
{*******************************************************************************
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;