I'm using a TEdit
In that case i am confused a little.
Why ? because that would mean, following your reasoning, that in case user inputs -15,00 it should be automagically displayed as (15,00) ?
That is confusing for end-user, to say the least.
But, perhaps you only want to allow different input (e.g. brackets would be automatically converted into the correct internal storage).
Your phrase 'simple numeric display components' makes me think that there ARE edit components that don't use strings
Don't worry about being new to Lazarus, we all were at one point :-) (i still am for that matter).
I asked because there are many 3th party components out there. For all i know you are just using a grid to display your values (which requires a somewhat different scenario).
In any case it still needs a string to display.
Of course but, it was not clear from your original question that you required both input and output. Different circumstance, different approach
Just like Eugene, i never used formatfloat, but that seems like a good starting point (at least for displaying).
That's an interesting possibility - - - not that I have any real idea as to how to go about it!
I'm still stuck with the need to do an explicit conversion. I thought that implicit conversion worked for record class operators (in opposite to global operators) so i must be (doing something) wrong there.
e.g. the best i can do atm would be something alike:
var
x : TAccountancyNumber;
begin
WriteLn('x := ', String(x));
end.
Were the number x automatically displayed in brackets when negative and without brackets when positive. Also a string can be assigned to such TAccountacynumber and act on brackets being present (or not present).
In case you already are using your own conversion functions then there would be not much to gain. Stuffing things into your own (record) type helps a little with regards to 'hiding' things from plain sight but, that is all it does (and in hindsight i just could have use global operators for that).
program test;
{$MODE OBJFPC}{$H+}
{$MODESWITCh ADVANCEDRECORDS}
Uses
SysUtils, StrUtils;
Type
TAccountancyNumber = record
class operator := (source: Double): TAccountancyNumber;
class operator := (source: TAccountancyNumber): String;
class operator := (source: String): TAccountancyNumber;
private
Number : Double;
public
end;
class operator TAccountancyNumber.:= (source: Double): TAccountancyNumber;
begin
Result.Number := Source;
end;
class operator TAccountancyNumber.:= (source: TAccountancyNumber): String;
begin
if (Source.number < 0.0)
then Result := '(' + FloatToStr(Source.number) + ')'
else Result := FloatToStr(Source.number);
end;
class operator TAccountancyNumber.:= (source: string): TAccountancyNumber;
begin
if ( (LeftStr(source,1) = '(') and (RightStr(source,1) = ')') )
then Result.Number := -StrToFloat(TrimSet(source, ['(',')'] ) )
else Result.Number := StrToFloat(source)
end;
var
k : double;
l : double;
x : TAccountancyNumber;
y : TAccountancyNumber;
begin
k := -25.00;
l := 30.00;
x := -15.00;
y := 138.00;
WriteLn('k := ', k);
WriteLn('l := ', l);
WriteLn('x := ', String(x));
WriteLn('y := ', String(y));
x := '(40)';
WriteLn('x := ', String(x));
end.
There are no details on precision as that is yours to decide. At least it shows a little how to go about such a thing (at least, i hope it does)
With regards tio your inputting. Perhaps a
tmaskedit would be able to help you out with regards to allowed input characters (at least it is one thing less to worry about).
edit: copy-paste error -> forgot to show switch and uses clause