Formatierungsfunktionen machen einen großen Teil von SysUtils aus. Einige von ihnen sind verschachtelt und bestehen aus ca. 1000 Zeilen Delphi Code. Dennoch funktioniert der nach C# übersetzte Code nahezu perfekt. 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 Delphi2Cpp 2.x ohne nachträgliche manuelle Bearbeitung kompilieren und fehlerfrei ausführen.
using System.Sysutils; using System; using static Dbsc_format.Dbsc_formatImplementation; using static Dbsc_format.Dbsc_formatInterface; using static System.SystemInterface; using static System.Sysutils.SysutilsInterface;
namespace Dbsc_format
{
public class Dbsc_formatInterface
{
//http://www.delphibasics.co.uk/RTL.asp?Name=Format
//http://www.delphibasics.co.uk/RTL.asp?Name=FloatToStr
//http://www.delphibasics.co.uk/RTL.asp?Name=FormatFloat
public static bool FormatTest()
{
bool result = false;
result = true;
result = result && FormatTest1();
result = result && FormatTest2();
result = result && FloatToStrTest1();
result = result && FormatFloatTest1();
result = result && Test_FormatPointer();
return result;
}
} // class Dbsc_formatInterface
file class Dbsc_formatImplementation
{
public static bool FormatTest1()
{
bool result = false;
TDateTime easter = new TDateTime();
string s = string.Empty;
result = true;
// easter := EncodeDate(2024, 3, 31) + EncodeTime(12, 0,0,0);
// s := Format('%2:u', [easter]);
// Just 1 data item
result = result && (Format("%s", new TVarRec[] { "Hello" }) == "Hello");
// A mix of literal text and a data item
result = result && (Format("String = %s", new TVarRec[] { "Hello" }) == "String = Hello");
//ShowMessage('');
// Examples of each of the data types
result = result && (Format("Decimal = %d", new TVarRec[] { -123 }) == "Decimal = -123");
result = result && (Format("Exponent = %e", new TVarRec[] { 12345.678D }) == "Exponent = 1,23456780000000E+004");
result = result && (Format("Fixed = %f", new TVarRec[] { 12345.678D }) == "Fixed = 12345,68");
result = result && (Format("General = %g", new TVarRec[] { 12345.678D }) == "General = 12345,678");
result = result && (Format("Number = %n", new TVarRec[] { 12345.678D }) == "Number = 12.345,68");
result = result && (Format("Money = %m", new TVarRec[] { 12345.678D }) == "Money = 12.345,68 €");
// makes no sense under C#
// result := result and (Format('Pointer = %p', [addr(text)]) = 'Pointer = 0069FC90');
result = result && (Format("String = %s", new TVarRec[] { "Hello" }) == "String = Hello");
result = result && (Format("Unsigned decimal = %u", new TVarRec[] { 123 }) == "Unsigned decimal = 123");
result = result && (Format("Hexadecimal = %x", new TVarRec[] { 140 }) == "Hexadecimal = 8C");
return result;
}
public static 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("Padded decimal = <%7d>", new TVarRec[] { 1234 }) == "Padded decimal = < 1234>");
// With the '-' operator, the data is left justified
result = result && (Format("Justified decimal = <%-7d>", new TVarRec[] { 1234 }) == "Justified decimal = <1234 >");
// The precision value forces 0 padding to the desired size
result = result && (Format("0 padded decimal = <%.6d>", new TVarRec[] { 1234 }) == "0 padded decimal = <001234>");
// A combination of width and precision
// Note that width value precedes the precision value
result = result && (Format("Width + precision = <%8.6d>", new TVarRec[] { 1234 }) == "Width + precision = < 001234>");
// The index value allows the next value in the data array
// to be changed
result = result && (Format("Reposition after 3 strings = %s %s %s %1:s %s", new TVarRec[] { "Zero", "One", "Two", "Three" }) == "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("In line = <%10.4d>", new TVarRec[] { 1234 }) == "In line = < 1234>");
result = result && (Format("Part data driven = <%*.4d>", new TVarRec[] { 10, 1234 }) == "Part data driven = < 1234>");
result = result && (Format("Data driven = <%*.*d>", new TVarRec[] { 10, 4, 1234 }) == "Data driven = < 1234>");
return result;
}
public static bool FloatToStrTest1()
{
bool result = false;
double amount1 = 0.0D;
double amount2 = 0.0D;
double amount3 = 0.0D;
result = true;
amount1 = 1234567890.123456789D; // High precision number
amount2 = 1234567890123456.123D; // High mantissa digits
amount3 = 1E100D; // High value number
result = result && (FloatToStr(amount1) == "1234567890,12346");
result = result && (FloatToStr(amount2) == "1,23456789012346E15");
result = result && (FloatToStr(amount3) == "1E100");
return result;
}
public static bool FormatFloatTest1()
{
bool result = false;
double flt = 0.0D;
result = true;
// Set up our floating point number
flt = 1234.567D;
// Display a sample value using all of the format options
// Round out the decimal value
result = result && (FormatFloat("#####", flt) == "1235");
result = result && (FormatFloat("00000", flt) == "01235");
result = result && (FormatFloat("0", flt) == "1235");
result = result && (FormatFloat("#,##0", flt) == "1.235");
result = result && (FormatFloat(",0", flt) == "1.235");
// Include the decimal value
result = result && (FormatFloat("0.####", flt) == "1234,567");
result = result && (FormatFloat("0.0000", flt) == "1234,5670");
// Scientific format
result = result && (FormatFloat("0.0000000E+00", flt) == "1,2345670E+03");
result = result && (FormatFloat("0.0000000E-00", flt) == "1,2345670E03");
result = result && (FormatFloat("#.#######E-##", flt) == "1,234567E3");
// Include freeform text
result = result && (FormatFloat("\"Value = \"0.0", flt) == "Value = 1234,6");
// Different formatting for negative numbers
result = result && (FormatFloat("0.0", -1234.567D) == "-1234,6");
result = result && (FormatFloat("0.0 \"CR\";0.0 \"DB\"", -1234.567D) == "1234,6 DB");
result = result && (FormatFloat("0.0 \"CR\";0.0 \"DB\"", 1234.567D) == "1234,6 CR");
// Different format for zero value
result = result && (FormatFloat("0.0", 0.0D) == "0,0");
result = result && (FormatFloat("0.0;-0.0;\"Nothing\"", 0.0D) == "Nothing");
return result;
}
public static bool Test_FormatPointer()
{
bool result = false;
Pointer ptr = default;
string formattedStr = string.Empty;
ptr = ((Pointer)0x1234ABCD); // Example pointer value for testing
formattedStr = Format("Pointer address: %p", new TVarRec[] { ptr });
// In Delphi 7, Format with "%p" may format the pointer as hexadecimal.
// Expected output might vary, but it will be in a hexadecimal string format.
result = formattedStr == "Pointer address: 1234ABCD";
return result;
}
} // class Dbsc_formatImplementation
} // namespace Dbsc_format
English
| Letzte Neuigkeiten |
|
01.09.26
Delphi2C# 2.8.0: Grundlegend überarbeitete Zeigersimulation [mehr...] |
|
18.05.26
Delphi2C# 2.7.0: Übersetzungsheuristiken [mehr...] |
|
Diese Homepage ist aus einfachen Texten mit
[Minimal Website]
generiert.
|
Minimal Website ist mit Hilfe des TextTransformers hergestellt.
|
Der TextTransformer ist gemacht mit dem Borland
C++Builder
|