Einzelnen Beitrag anzeigen

Benutzerbild von markus5766h
markus5766h

Registriert seit: 5. Mär 2009
Ort: Hamburg
569 Beiträge
 
Delphi XE8 Professional
 
#5

AW: Extended To String mit dekadischen Einheiten

  Alt 20. Mär 2016, 19:54
Moin.

eine etwas überarbeitete Version :

Delphi-Quellcode:

// Value : Wert [Extended]
// Short : True = kurze Exponententialwerte : 'K für Kilo' .... , False = Exponentialwerte ausgeschrieben
// OutString : Einheit zum Anhängen an die Ausgabe
// Accuracy : Genauigkeit
// Digits : NachkommaStellen
// Eliminate : True = Eliminierung der führenden Null bei -1 < Wert < 1 , aus 0,123Milli... wird 123Mikro..

function FloatToStrS(Value : Extended; Short, Eliminate : Boolean; OutString : String; accuracy, digits : Integer): String;
const
  ExportString : array[Boolean, 1..17] of String =
                       ((' Yokto', ' Zepto', ' Atto', ' Femto', ' Piko', ' Nano', ' Micro', ' Milli',
                       ' ', ' Kilo', ' Mega', ' Giga', ' Tera', ' Peta', ' Exa', ' Zetta', ' Yotta'),
                       (' y', ' z', ' a', ' f', ' p', ' n', ' µ', ' m',
                       ' ', ' K', ' M', ' G', ' T', ' P', ' E', ' Z', ' Y'));
var
  Exp : Integer;
  Range : Integer;
  ExpStep : Extended;
begin
  ExpStep := 1 / 1000;
  if (accuracy < 0) or (accuracy > 18) then accuracy := 3;
  if Value = 0 then
    begin
      Result := '0 '+OutString;
      Exit;
    end;

  if Value < 0 then Range := -1 else Range := 1;
  if ((Value > -1) and (Value < 1))
    then Exp := Trunc(ln(Value * Range) / ln(2) / 10 + ExpStep)
      else Exp := Trunc(ln((Value*10) * Range) / ln(2) / 10 + ExpStep)+1;

  if (Value >= 1E25) or ((1/Value) >= 1E25)
    then Result := 'out of Range'
      else
        if ((Eliminate) and (Value > -1) and (Value < 1) and (Exp > -8))
          then Result := FloatToStrF(Value*1000 / Power(10, Exp * 3), ffNumber, accuracy, digits) + ExportString[Short, Exp+8] + OutString
            else if not Eliminate then Result := FloatToStrF(Value / Power(10, Exp * 3), ffNumber, accuracy, digits) + ExportString[Short, Exp+9] + OutString
              else if Eliminate then Result := FloatToStrF(Value / Power(10, (Exp-1) * 3), ffNumber, accuracy, digits) + ExportString[Short, Exp+8] + OutString;
end;
Markus H.

Geändert von markus5766h (12. Nov 2016 um 21:12 Uhr)
  Mit Zitat antworten Zitat