Einzelnen Beitrag anzeigen

marabu

Registriert seit: 6. Apr 2005
10.109 Beiträge
 
#8

Re: Schleife für StringReplace

  Alt 14. Jun 2006, 10:54
Eine kleine Code-Spende:

Delphi-Quellcode:
const
  S_UNITS = 'pnµm kMGT';

function FloatToSym(value: Extended): String;
var
  i, sign: Integer;
  log: Extended;
begin
  log := Ln(value) / Ln(10);
  sign := 1 - Ord(log < 0) shl 1;
  i := 0;
  while (log < 0) or not (log < 3) do
  begin
    Inc(i, sign);
    log := log - sign * 3;
  end;
  value := Exp(log * Ln(10));
  Result := Format('%g%s', [value, S_UNITS[i + Pos(' ', S_UNITS)]]);
end;

function SymToFloat(s: String): Extended;
var
  i, index: Integer;
begin
  Result := 0;
  for i := 1 to Length(s) do
    if not TryStrToFloat(Copy(s, 1, i), Result) then
    begin
      index := Pos(s[i], S_UNITS);
      if index > 0 then
      begin
        Result := Exp(ln(Result) + (index - Pos(' ', S_UNITS)) * 3 * Ln(10));
        Exit;
      end;
    end;
end;
Grüße vom marabu
  Mit Zitat antworten Zitat