SRL Archive - Snippets

function int32.formatKMB():string;


Obscurity

2015-05-09 20:42:47

Nothing major. Just thought some of yous could use it in your proggies and such. Probably a better method of doing it, but meh. Rounds to the tenth.

function int32.formatKMB(funcDecimal:int32=1):string;
begin
case self of
0..999:result:=toStr(self);
1000..999999:result:=toStr(round(self/pow(10,3-(funcDecimal:=min(3,funcDecimal))))/pow(10,funcDecimal))+'K';
1000000..999999999:result:=toStr(round(self/pow(10,6-(funcDecimal:=min(3,funcDecimal))))/pow(10,funcDecimal))+'M';
1000000000..2147483647:result:=toStr(round(self/pow(10,9-(funcDecimal:=min(3,funcDecimal))))/pow(10,funcDecimal))+'B';
end;
end;

function uInt32.formatKMB(funcDecimal:int32=1):string;
begin
case self of
0..999:result:=toStr(self);
1000..999999:result:=toStr(round(self/pow(10,3-(funcDecimal:=min(3,funcDecimal))))/pow(10,funcDecimal))+'K';
1000000..999999999:result:=toStr(round(self/pow(10,6-(funcDecimal:=min(3,funcDecimal))))/pow(10,funcDecimal))+'M';
1000000000..2147483647:result:=toStr(round(self/pow(10,9-(funcDecimal:=min(3,funcDecimal))))/pow(10,funcDecimal))+'B';
end;
end;

moneyEarned:=12345678;

writeLN('You earned ',moneyEarned.formatKMB(),' GP!'); //~ "You earned 12.3M GP!"

rj

2015-05-09 23:02:58

Heh, this reminds me of my function I used to color cash on my simba black jack game (warning: old code)

function cashString():String;
begin
result := ToStr(myPlayer.cash);
if ((myPlayer.cash > 99999) and (myPlayer.cash < 9999999)) then
result := ToStr((Round(myPlayer.cash / 1000))) + 'K';
if ((myPlayer.cash > 9999999) and (myPlayer.cash < 9999999999)) then
result := ToStr((Round(myPlayer.cash / 1000000))) + 'M';
if ((myPlayer.cash > 9999999999) and (myPlayer.cash < 9999999999999)) then
result := ToStr((Round(myPlayer.cash / 1000000000))) + 'B';
if ((myPlayer.cash > 9999999999999) and (myPlayer.cash <= 9999999999999999)) then
result := ToStr((Round(myPlayer.cash / 1000000000000))) + 'T';
if ((myPlayer.cash > 9999999999999999) and (myPlayer.cash <=9223372036854775807)) then
result := ToStr((Round(myPlayer.cash / 1000000000000000))) + 'Q';
end;

Obscurity

2015-05-09 23:28:57

Would have taken that approach if I hadn't known about case. :P.

Also updated the OP. You can now tell it how many decimal points you'd like:
moneyEarned:=12345678;

writeLN('You earned ',moneyEarned.formatKMB(),' GP!'); //~ "You earned 12.3M GP!"
writeLN('You earned ',moneyEarned.formatKMB(2),' GP!'); //~ "You earned 12.35M GP!"
writeLN('You earned ',moneyEarned.formatKMB(4),' GP!'); //~ "You earned 12.3457M GP!"
writeLN('You earned ',moneyEarned.formatKMB(0),' GP!'); //~ "You earned 12M GP!"

Justin

2015-05-09 23:34:17

Thanks Obscurity, will use this :)

masterBB

2015-05-10 14:51:00

Not exactly the same but:

function int32.formatKMB():string;
begin
result := IntToStr(floor(self / Pow(10, 3 * Max(Min(Floor((log10(Max(self, 1))-1)/3), 2),0)))) + TCharArray([' ', 'K', 'M'])[Min(Floor((log10(Max(self, 1))-1)/3), 2)];
end;

(posted before)

Obscurity

2015-05-10 15:34:09

That's great! I wasn't aware of log10 - just mistakenly keep assuming that Lape lacks functionality that it actually has.

Another method I though was fun was using:
Self/Pow(10,Length(ToStr(Self))-1)

On phone - sorry. :-P.