Format

Top  Previous  Next

Unit Tests > Format

The formatting routines account for a considerable part of the SysUtils unit. Some of them are nested and consist in about 1000 lines of code. Nevertheless the translated code works nearly perfect. Examples to the formatting routines from

 

http://www.delphibasics.co.uk/RTL.asp?Name=format

 

 

were modified slightly to be able to use them for test purposes. The code translated with Delphi2C# compiles and works without additional manual processing without faults.

 

using static dbsc_format.dbsc_formatInterface;

using static dbsc_format.dbsc_formatImplementation;

using System;

using static System.SystemInterface;

using System.SysUtils;

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();

  return result;

}

 

} // class dbsc_formatInterface

 

 

public class dbsc_formatImplementation

{

 

public static bool FormatTest1()

{

  bool result = false;

  result = true;

  // Just 1 data item

  result = result && (Format("%s", new object[]{"Hello"}) == "Hello");

 

  // A mix of literal text and a data item

  result = result && (Format("String = %s", new object[]{"Hello"}) == "String = Hello");

   //ShowMessage('');

 

  // Examples of each of the data types

  result = result && (Format("Decimal          = %d", new object[]{-123}) == "Decimal          = -123");

  result = result && (Format("Exponent         = %e", new object[]{12345.678D}) == "Exponent         = 1,23456780000000E+004");

  result = result && (Format("Fixed            = %f", new object[]{12345.678D}) == "Fixed            = 12345,68");

  result = result && (Format("General          = %g", new object[]{12345.678D}) == "General          = 12345,678");

  result = result && (Format("Number           = %n", new object[]{12345.678D}) == "Number           = 12.345,68");

  result = result && (Format("Money            = %m", new object[]{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 object[]{"Hello"}) == "String           = Hello");

  result = result && (Format("Unsigned decimal = %u", new object[]{123}) == "Unsigned decimal = 123");

  result = result && (Format("Hexadecimal      = %x", new object[]{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 object[]{1234}) == "Padded decimal    = <   1234>");

 

  // With the '-' operator, the data is left justified

  result = result && (Format("Justified decimal = <%-7d>", new object[]{1234}) == "Justified decimal = <1234   >");

 

  // The precision value forces 0 padding to the desired size

  result = result && (Format("0 padded decimal  = <%.6d>", new object[]{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 object[]{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 object[]{"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 object[]{1234}) == "In line           = <      1234>");

  result = result && (Format("Part data driven  = <%*.4d>", new object[]{10, 1234}) == "Part data driven  = <      1234>");

  result = result && (Format("Data driven       = <%*.*d>", new object[]{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;

}

 

} // class dbsc_formatImplementation

 

}  // namespace dbsc_format

 

 



This page belongs to the Delphi2C# Documentation

Delphi2C# home  Content