Mega Code Archive

 
Categories / Delphi / Strings
 

Add thousandths seperator (not forgetting the digits)

Title: Add thousandths-seperator (not forgetting the digits) Question: Hiow can I add a thousandths-seperator to numbers (real) Answer: I use a function that does this for me. It is short and clean. Another functions handles the digits (you can determin how many digits to be used. -- I use a tSpinEdit that sets the number of digiyts) Omer Yasar Can -- omercan@home.nl unit Unit1; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, Buttons, ComCtrls, Spin; type TForm1 = class(TForm) BitBtn_thousandths: TBitBtn; Edit_thousandths: TEdit; SpinEdit_thousandths: TSpinEdit; StatusBar1: TStatusBar; procedure BitBtn_thousandthsClick(Sender: TObject); private { Private-Deklarationen } public { Public-Deklarationen } end; var Form1: TForm1; implementation {$R *.DFM} //code entered w. Delphi 5 function Ins(const SubStr : String; const s : String; Index : Integer = 3) : String; begin Result := s; System.Insert(SubStr, Result, Index); end; function AddThousandths (Entry: Extended; NumberOfDigits: integer): string; var s: string; i: integer; begin s := '#,##0.'; for i:=1 to NumberOfDigits do s := Ins('0',s,length(s)+1); // form1.Caption := s; Result := FormatFloat (s , (Entry)); { '#,##0.000' : 1.234,567 '#,##0.00' : 1.234,57 '#,##0.0' : 1.234,6 '#,##' : 1.235 } end; {==============================================================================} procedure TForm1.BitBtn_thousandthsClick(Sender: TObject); begin Edit_thousandths.Text := '1234,567'; //just reset... Edit_thousandths.Text := AddThousandths ( (strToFloat(Edit_thousandths.Text)), SpinEdit_thousandths.Value); end; end. Happy programming Omer