Formatierungsfunktionen machen einen großen Teil von SysUtils aus. Einige von ihnen sind verschachtelt und bestehen aus ca. 1000 Zeilen Delphi Code. Dennoch lassen sie sich naheu perfekt mit DelphiXE2Cpp11 nach C++ übersetzen. Beispiele zu den Formatierungsfunktionen von der Seite

Delphi Basics Format

wurden leicht modifiziert, um sie für Testzwecke einsetzen zu können. Der Code Lässt sich mit DelphiXE2Cpp11 ohne nachträgliche manuelle Bearbeitung kompilieren und fehlerfrei ausführen.


bool FormatTest1()
{
  bool result = false;
  result = true;
  // Just 1 data item
  result = result && (Format(L"%s", OpenArray(String(L"Hello"))) == L"Hello");

  // A mix of literal text and a data item
  result = result && (Format(L"String = %s", OpenArray(String(L"Hello"))) == L"String = Hello");
   //ShowMessage('');

  // Examples of each of the data types
  result = result && (Format(L"Decimal          = %d", OpenArray(-123)) == L"Decimal          = -123");
  result = result && (Format(L"Exponent         = %e", OpenArray(12345.678L)) == L"Exponent         = 1,23456780000000E+004");
  result = result && (Format(L"Fixed            = %f", OpenArray(12345.678L)) == L"Fixed            = 12345,68");
  result = result && (Format(L"General          = %g", OpenArray(12345.678L)) == L"General          = 12345,678");
  result = result && (Format(L"Number           = %n", OpenArray(12345.678L)) == L"Number           = 12.345,68");
  result = result && (Format(L"Money            = %m", OpenArray(12345.678L)) == L"Money            = 12.345,68 €");
   // makes no sense under C#
   // result := result and (Format('Pointer          = %p', [addr(text)]) = 'Pointer          = 0069FC90');
  result = result && (Format(L"String           = %s", OpenArray(String(L"Hello"))) == L"String           = Hello");
  result = result && (Format(L"Unsigned decimal = %u", OpenArray(123)) == L"Unsigned decimal = 123");
  result = result && (Format(L"Hexadecimal      = %x", OpenArray(140)) == L"Hexadecimal      = 8C");
  return result;
}

bool FormatTest2()
{
  bool result = false;
  result = true;
  // The width value dictates the output size
  // with blank padding to the left
  // Note the <> characters are added to show formatting
  result = result && (Format(L"Padded decimal    = <%7d>", OpenArray(1234)) == L"Padded decimal    = <   1234>");

  // With the '-' operator, the data is left justified
  result = result && (Format(L"Justified decimal = <%-7d>", OpenArray(1234)) == L"Justified decimal = <1234   >");

  // The precision value forces 0 padding to the desired size
  result = result && (Format(L"0 padded decimal  = <%.6d>", OpenArray(1234)) == L"0 padded decimal  = <001234>");

  // A combination of width and precision
  // Note that width value precedes the precision value
  result = result && (Format(L"Width + precision = <%8.6d>", OpenArray(1234)) == L"Width + precision = <  001234>");

  // The index value allows the next value in the data array
  // to be changed
  result = result && (Format(L"Reposition after 3 strings = %s %s %s %1:s %s", OpenArray(String(L"Zero"), String(L"One"), String(L"Two"), String(L"Three"))) == L"Reposition after 3 strings = Zero One Two One Two");

  // One or more of the values may be provided by the
  // data array itself. Note that testing has shown that an *
  // for the width parameter can yield EConvertError.
  result = result && (Format(L"In line           = <%10.4d>", OpenArray(1234)) == L"In line           = <      1234>");
  result = result && (Format(L"Part data driven  = <%*.4d>", OpenArray(10, 1234)) == L"Part data driven  = <      1234>");
  result = result && (Format(L"Data driven       = <%*.*d>", OpenArray(10, 4, 1234)) == L"Data driven       = <      1234>");
  return result;
}

bool FloatToStrTest1()
{
  bool result = false;
  long double amount1 = 0.0L;
  long double amount2 = 0.0L;
  long double amount3 = 0.0L;
  result = true;
  amount1 = 1234567890.123456789L;  // High precision number
  amount2 = 1234567890123456.123L;  // High mantissa digits
  amount3 = 1E100L;                 // High value number
  result = result && (FloatToStr(amount1) == L"1234567890,12346");
  result = result && (FloatToStr(amount2) == L"1,23456789012346E15");
  result = result && (FloatToStr(amount3) == L"1E100");
  return result;
}

bool FormatFloatTest1()
{
  bool result = false;
  long double flt = 0.0L;
  result = true;
  // Set up our floating point number
  flt = 1234.567L;

  // Display a sample value using all of the format options

  // Round out the decimal value
  result = result && (FormatFloat(L"#####", flt) == L"1235");
  result = result && (FormatFloat(L"00000", flt) == L"01235");
  result = result && (FormatFloat(L"0", flt) == L"1235");
  result = result && (FormatFloat(L"#,##0", flt) == L"1.235");
  result = result && (FormatFloat(L",0", flt) == L"1.235");

  // Include the decimal value
  result = result && (FormatFloat(L"0.####", flt) == L"1234,567");
  result = result && (FormatFloat(L"0.0000", flt) == L"1234,5670");

  // Scientific format
  result = result && (FormatFloat(L"0.0000000E+00", flt) == L"1,2345670E+03");
  result = result && (FormatFloat(L"0.0000000E-00", flt) == L"1,2345670E03");
  result = result && (FormatFloat(L"#.#######E-##", flt) == L"1,234567E3");

  // Include freeform text
  result = result && (FormatFloat(L"\"Value = \"0.0", flt) == L"Value = 1234,6");

  // Different formatting for negative numbers
  result = result && (FormatFloat(L"0.0", -1234.567) == L"-1234,6");
  result = result && (FormatFloat(L"0.0 \"CR\";0.0 \"DB\"", -1234.567) == L"1234,6 DB");
  result = result && (FormatFloat(L"0.0 \"CR\";0.0 \"DB\"", 1234.567L) == L"1234,6 CR");

  // Different format for zero value
  result = result && (FormatFloat(L"0.0", 0.0L) == L"0,0");
  result = result && (FormatFloat(L"0.0;-0.0;\"Nothing\"", 0.0L) == L"Nothing");
  return result;
}

bool FormatTest()
{
  bool result = false;
  result = true;
  result = result && FormatTest1();
  result = result && FormatTest2();
  result = result && FloatToStrTest1();
  result = result && FormatFloatTest1();
  return result;
}

   english english

 
Letzte Neuigkeiten
05.08.2020
DelphiXE2Cpp11 Verbesserte Nachbilung der Sichtbarkeit von Klassenmembern [mehr...]

23.06.20
DelphiXE2Cpp11 Übersetzungs-Service [mehr...]