6

This is my procedure.

procedure format_integer_field(Atable: TDataSet);
 var i: integer;
begin
 if Atable.Active then
 if Atable.FieldCount > 0 then
 with Atable do
 begin
  for i:= 0 to FieldCount-1 do
  if (Fields[i] is TIntegerField) then
  begin
   (Fields[i] as TIntegerField).DisplayFormat := '###,###';
   (Fields[i] as TIntegerField).EditFormat := '#';
  end
  else
   if (Fields[i] is TFloatField) then
  begin
   (Fields[i] as TFloatField).DisplayFormat := '###,###.##';
   (Fields[i] as TFloatField).EditFormat := '#.##';
  end;
 end;
end;

This is work fine until a number like "0.9" has been entered and result will be ".9". How can I have thousand separator and zero before floating point that smaller than "1".

Arioch 'The
  • 15,799
  • 35
  • 62
Morteza Samie
  • 125
  • 1
  • 1
  • 9
  • I can understand wanting a zero, but a thousands separator for a number that only has one digit before the decimal separator, are you sure you want that? It would make 0.9 look like 0,000.9. –  Jan 28 '14 at 08:05
  • @hvd maybe that is the way he tries to align numbers "by the comma" or by any other strategy – Arioch 'The Jan 28 '14 at 08:17
  • @Arioch'The Considering the OP likes the behaviour for other numbers (1 doesn't become 0,001), I doubt that, but yes, it's possible. –  Jan 28 '14 at 08:23
  • @hvd Thanks, I didn't notice that 0,000.9 and its will be bigger problem. I just want my numbers looks good. – Morteza Samie Jan 28 '14 at 08:40
  • Does `###,##0.0#` do it for you ? – Hugh Jones Jan 28 '14 at 09:28
  • @HughJones Please Write it as an answer. – Morteza Samie Jan 29 '14 at 02:54

2 Answers2

5

Try (Fields[i] as TFloatField).DisplayFormat := '##0,000.00';

As you did read in documentation at http://docwiki.embarcadero.com/RADStudio/XE3/en/Using_Default_Formatting_for_Numeric,_Date,_and_Time_Fields it says

Default formatting is performed by the following routines:

  • FormatFloat -- TFloatField, TCurrencyField

And how you did read in the following documentation pages

the documentation quotes

  • 0 -> Digit placeholder. If the value being formatted has a digit in the position where '0' appears in the format string, then that digit is copied to the output string. Otherwise, a '0' is stored in that position in the output string.
  • # -> Digit placeholder. If the value being formatted has a digit in the position where '#' appears in the format string, then that digit is copied to the output string. Otherwise, nothing is stored in that position in the output string.

So by using "#" in the formatting pattern you tell Delphi "i do not need any digits (and thousands separators with them) in this place, but you might put them if you want" - and since Delphi does not want to put leading zeros - you don't have any. However, if you really need those digits and the thousands separator with them, you put "0" instead of "#" and that way you tell Delphi "the digits just need to be here, whether you want to put them or not"

Arioch 'The
  • 15,799
  • 35
  • 62
4

The format you need is ###,##0.0#

Hugh Jones
  • 2,706
  • 19
  • 30