Advertisement
WarPie90

FormatNum - rs style

Oct 14th, 2015
375
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 1.00 KB | None | 0 0
  1. function FormatNum(n:Int64; dec:Byte=3): String;
  2. var i:Int32; f:Double:=n;
  3. begin
  4.   while (abs(f) >= 1000) and Inc(i) do f /= 1000.0;
  5.   Result := FloatToStr(Round(f,dec)) + (['','k','m','b','t','qd','qt'])[i-1];
  6. end;
  7.  
  8. function InvFormatNum(str:String): Int64;
  9. var
  10.   i:Int32;
  11.   n:Double;
  12.   format  := ExtractFromStr(str, Letters);
  13.   formats := ['','k','m','b','t','qd','qt'];
  14. begin
  15.   SetLength(str, Length(str)-Length(format));
  16.   n := StrToFloat(str);
  17.   while (formats[i] <> format) and Inc(i) do n *= 1000.0;
  18.   Result := Round(n);
  19. end;
  20.  
  21. begin
  22.   WriteLn(FormatNum(0));
  23.   WriteLn(FormatNum(500));
  24.   WriteLn(FormatNum(1500));
  25.   WriteLn(FormatNum(1200000));
  26.   WriteLn(FormatNum(1601000000));
  27.   WriteLn(FormatNum(-1500));
  28.  
  29.   // reversed
  30.   WriteLn(InvFormatNum(FormatNum(0)));
  31.   WriteLn(InvFormatNum(FormatNum(500)));
  32.   WriteLn(InvFormatNum(FormatNum(1500)));
  33.   WriteLn(InvFormatNum(FormatNum(1200000)));
  34.   WriteLn(InvFormatNum(FormatNum(1601000000)));
  35.   WriteLn(InvFormatNum(FormatNum(-1500)));
  36. end;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement