Advertisement
djjd47130

Delphi Integer Ordinal Suffix

Feb 4th, 2015
467
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 2.11 KB | None | 0 0
  1. { DFM }
  2.  
  3. object Form1: TForm1
  4.   Left = 0
  5.   Top = 0
  6.   Caption = 'Form1'
  7.   ClientHeight = 214
  8.   ClientWidth = 266
  9.   Color = clBtnFace
  10.   Font.Charset = DEFAULT_CHARSET
  11.   Font.Color = clWindowText
  12.   Font.Height = -11
  13.   Font.Name = 'Tahoma'
  14.   Font.Style = []
  15.   OldCreateOrder = False
  16.   PixelsPerInch = 96
  17.   TextHeight = 13
  18.   object Label1: TLabel
  19.     Left = 88
  20.     Top = 112
  21.     Width = 31
  22.     Height = 13
  23.     Caption = 'Label1'
  24.   end
  25.   object Button1: TButton
  26.     Left = 80
  27.     Top = 64
  28.     Width = 75
  29.     Height = 25
  30.     Caption = 'Button1'
  31.     TabOrder = 0
  32.     OnClick = Button1Click
  33.   end
  34.   object SpinEdit1: TSpinEdit
  35.     Left = 64
  36.     Top = 24
  37.     Width = 121
  38.     Height = 22
  39.     MaxValue = 0
  40.     MinValue = 0
  41.     TabOrder = 1
  42.     Value = 42
  43.   end
  44. end
  45.  
  46. { PAS }
  47.  
  48. unit uMain;
  49.  
  50. interface
  51.  
  52. uses
  53.   Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  54.   Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.Samples.Spin;
  55.  
  56. type
  57.   TForm1 = class(TForm)
  58.     Button1: TButton;
  59.     Label1: TLabel;
  60.     SpinEdit1: TSpinEdit;
  61.     procedure Button1Click(Sender: TObject);
  62.   private
  63.     { Private declarations }
  64.   public
  65.     { Public declarations }
  66.   end;
  67.  
  68. var
  69.   Form1: TForm1;
  70.  
  71. implementation
  72.  
  73. {$R *.dfm}
  74.  
  75. const
  76.   Ends: array[0..9] of String = ('th','st','nd','rd','th','th','th','th','th','th');
  77.  
  78. function IntSuffix(const Number: Integer; const AddSpace: Boolean = False;
  79.   const SuffixOnly: Boolean = False): String;
  80. begin
  81.   if not SuffixOnly then begin
  82.     Result:= IntToStr(Number);
  83.     if AddSpace then Result:= Result + ' ';
  84.   end;
  85.   if ((Number mod 100) >= 11) and ((Number mod 100) <= 13) then
  86.     Result:= IntToStr(Number) + 'th'
  87.   else
  88.     Result:= IntToStr(Number) + Ends[Number mod 10];
  89. end;
  90.  
  91. (*
  92. $ends = array('th','st','nd','rd','th','th','th','th','th','th');
  93. if (($number %100) >= 11 && ($number%100) <= 13)
  94.    $abbreviation = $number. 'th';
  95. else
  96.    $abbreviation = $number. $ends[$number % 10];
  97. *)
  98.  
  99. procedure TForm1.Button1Click(Sender: TObject);
  100. begin
  101.   Label1.Caption:= IntSuffix(SpinEdit1.Value);
  102. end;
  103.  
  104. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement