From Clomosy Docs

No edit summary
No edit summary
Line 1: Line 1:
<div class="alert alert-ligth border border-3 border-primary-subtle rounded-5 p-4 shadow-sm" role="alert">
<div class="alert alert-ligth border border-3 border-primary-subtle rounded-5 p-4 shadow-sm" role="alert">
function FormatDateTime(const Format: string; DateTime: TclDateTime):string;
function FormatFloat(const Format: string; Value: Double): string;
</div>
</div>


The FormatDateTime function provides rich formatting of a TclDateTime value DateTime into a string. Formatting is defined by the Format string. The Format string can comprise a mix of ordinary characters (that are passed unchanged to the result string), and data formatting characters. This formatting is best explained by the example code.<br>
The FormatFloat function converts a numeric value (Double or Float) into a string according to the given Format string. The format string defines how the number will appear in the output. It can contain numeric formatting characters, optional placeholders, separators, scientific notation, percentages, and literal text.
<br>
   
   
The following (non-Asian) formatting character strings can be used in the Formatting string:<br>
   
   
:<b>y</b> = Year last 2 digits
:<b>0 Mandatory digit</b> = Always shows a digit; if missing, filled with 0
:<b>yy</b> = Year last 2 digits
:<b># Optional digit</b> = Shows a digit if present; otherwise left blank
:<b>yyyy</b> = Year as 4 digits
:<b>. Decimal separator</b> = Marks the decimal point (system locale dependent)
:<b>, Thousands separator</b> = Groups digits in thousands (system locale dependent)
:<b>E+00 / E-00 Scientific notation</b> = Shows the number in exponential format
:<b>% Percent</b> = Multiplies the number by 100 and adds a % sign
:<b>; Positive/negative separator</b> = Allows different formats for positive and negative numbers
:<b>'text' Literal text</b> = Displays the text exactly as written
:<b>\ Escape character</b> = Displays the next character literally


:<b>m</b> = Month number no-leading 0
<b>Example</b><br>
:<b>mm</b> = Month number as 2 digits
 
:<b>mmm</b> = Month using ShortDayNames (Jan)
<pre>
:<b>mmmm</b> = Month using LongDayNames (January)
var
    myNumber: Double;
 
{
    myNumber = 12345.6789;
 
    // Mandatory integer
    ShowMessage('0 = ' + FormatFloat('0', myNumber));
 
    // Two decimal places
    ShowMessage('0.00 = ' + FormatFloat('0.00', myNumber));
 
    // Optional decimal places
    ShowMessage('#.### = ' + FormatFloat('#.###', myNumber));
 
    // Thousands separator for integer
    ShowMessage('#,##0 = ' + FormatFloat('#,##0', myNumber));


:<b>d</b> = Day number no-leading 0
    // Thousands separator + 2 decimals
:<b>dd</b> = Day number as 2 digits
    ShowMessage('#,##0.00 = ' + FormatFloat('#,##0.00', myNumber));
:<b>ddd</b> = Day using ShortDayNames (Sun)
:<b>dddd</b> = Day using LongDayNames  (Sunday)
:<b>ddddd</b> = Day in ShortDateFormat
:<b>dddddd</b> = Day in LongDateFormat


:<b>c</b> = Use ShortDateFormat + LongTimeFormat
    // Percent format
:<b>h</b> = Hour number no-leading 0
    ShowMessage('0.00% = ' + FormatFloat('0.00%', myNumber/100));
:<b>hh</b> = Hour number as 2 digits
:<b>n</b> = Minute number no-leading 0
:<b>nn</b> = Minute number as 2 digits
:<b>s</b> = Second number no-leading 0
:<b>ss</b> = Second number as 2 digits
:<b>z</b> = Milli-sec number no-leading 0s
:<b>zzz</b> = Milli-sec number as 3 digits
:<b>t</b> = Use ShortTimeFormat
:<b>tt</b> = Use LongTimeFormat


:<b>am/pm</b> = Use after h : gives 12 hours + am/pm
    // Scientific notation
:<b>a/p</b> = Use after h : gives 12 hours + a/p
    ShowMessage('0.00E+00 = ' + FormatFloat('0.00E+00', myNumber));
:<b>ampm</b> = As a/p but TimeAMString,TimePMString
:<b>/</b> = Substituted by DateSeparator value
:<b>:</b> = Substituted by TimeSeparator value


<b>Example</b><br>
    // Negative number format
    myNumber = -12345.6789;
    ShowMessage('0;(#) = ' + FormatFloat('0;(#)', myNumber));


<pre>
     // Literal text
var
     myNumber = 42.5;
     myDate : TDateTime;
     ShowMessage('Value: 0.00 units = ' + FormatFloat('Value: 0.00 units', myNumber));
}
{
     myDate = '20.02.2023 12:24:26';
     ShowMessage('d.m.y = '+FormatDateTime('d.m.y', myDate));
    ShowMessage('dd.mm.yy = '+FormatDateTime('dd.mm.yy', myDate));
    // Use short names for the day, month, and add freeform text ('of')
    ShowMessage('ddd d of mmm yyyy = '+FormatDateTime('ddd d of mmm yyyy', myDate));
    // Use long names for the day and month
    ShowMessage('dddd d of mmmm yyyy = '+FormatDateTime('dddd d of mmmm yyyy', myDate));
    // Use the ShortDateFormat settings only
    ShowMessage('ddddd = '+FormatDateTime('ddddd', myDate));
    // Use the LongDateFormat settings only
    ShowMessage('dddddd = '+FormatDateTime('dddddd', myDate));
    ShowMessage('c = '+FormatDateTime('c', myDate));
}
</pre>
</pre>


<b>Output:</b><br>
<b>Output:</b><br>
<div class="alert alert-success" role="alert" data-bs-theme="light">
<div class="alert alert-success" role="alert" data-bs-theme="light">
d.m.y = 20.2.23<br>
0 = 12346<br>
dd.mm.yy = 20.02.23<br>
0.00 = 12345.68<br>
ddd d of mmm yyyy = Mon 20 of Feb 2023<br>
#.### = 12345.679<br>
dddd d of mmmm yyyy = Monday 20 of February 2023<br>
#,##0 = 12,346<br>
ddddd = 20.02.2023<br>
#,##0.00 = 12,345.68<br>
dddddd = 20 February 2023 Monday<br>
0.00% = 123.46%<br>
c = 20.02.2023 12:24:26
0.00E+00 = 1.23E+04<br>
0;(#) = (12346)<br>
Value: 0.00 units = 42.50 units
</div>
</div>



Revision as of 11:47, 16 October 2025

The FormatFloat function converts a numeric value (Double or Float) into a string according to the given Format string. The format string defines how the number will appear in the output. It can contain numeric formatting characters, optional placeholders, separators, scientific notation, percentages, and literal text.


0 Mandatory digit = Always shows a digit; if missing, filled with 0
# Optional digit = Shows a digit if present; otherwise left blank
. Decimal separator = Marks the decimal point (system locale dependent)
, Thousands separator = Groups digits in thousands (system locale dependent)
E+00 / E-00 Scientific notation = Shows the number in exponential format
% Percent = Multiplies the number by 100 and adds a % sign
; Positive/negative separator = Allows different formats for positive and negative numbers
'text' Literal text = Displays the text exactly as written
\ Escape character = Displays the next character literally

Example

var
    myNumber: Double;

{
    myNumber = 12345.6789;

    // Mandatory integer
    ShowMessage('0 = ' + FormatFloat('0', myNumber));

    // Two decimal places
    ShowMessage('0.00 = ' + FormatFloat('0.00', myNumber));

    // Optional decimal places
    ShowMessage('#.### = ' + FormatFloat('#.###', myNumber));

    // Thousands separator for integer
    ShowMessage('#,##0 = ' + FormatFloat('#,##0', myNumber));

    // Thousands separator + 2 decimals
    ShowMessage('#,##0.00 = ' + FormatFloat('#,##0.00', myNumber));

    // Percent format
    ShowMessage('0.00% = ' + FormatFloat('0.00%', myNumber/100));

    // Scientific notation
    ShowMessage('0.00E+00 = ' + FormatFloat('0.00E+00', myNumber));

    // Negative number format
    myNumber = -12345.6789;
    ShowMessage('0;(#) = ' + FormatFloat('0;(#)', myNumber));

    // Literal text
    myNumber = 42.5;
    ShowMessage('Value: 0.00 units = ' + FormatFloat('Value: 0.00 units', myNumber));
}

Output:

See Also