Recent

Author Topic: Accountancy format of 'Floats'  (Read 13110 times)

J-G

  • Hero Member
  • *****
  • Posts: 953
Accountancy format of 'Floats'
« on: January 10, 2017, 05:41:43 pm »
This is not an 'Error' that I can't work around, just a question as to the possibility of adding a facility to FPC.

It is the practice of accountants to put negative values in brackets   viz.  -£20.00  would be listed as (20.00)  -  often in Red as well.

I'm writing an invoicing package and a credit is naturally negative so I wrote a procedure to format the value, (bracketed)  -   but of course conversion from real life to display on screen needs a FloatToStr(Value) and '(20.00)' is an invalid 'Float'.

I haven't done an extensive search so it is possible that there is already a function other than FloatToStr that will handle this. If not, is it a worthwhile addition?
FPC 3.0.0 - Lazarus 1.6 &
FPC 3.2.2  - Lazarus 2.2.0 
Win 7 Ult 64

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Accountancy format of 'Floats'
« Reply #1 on: January 10, 2017, 06:00:57 pm »
Indeed brackets around a number make it a non number by default (being it float or otherwise).

Try to look at it from the other end, namely the component that displays the value. What are you currently using for that ?

If those are simple numeric display components then you might be able to tweak those to display the number(s) as you'd like them to be displayed.

In the case of your floattostr example, you can replace that with your own function, make the negative check and return the value bracketed. The opposite can also be done in case you wish to stay consistent. Easier would be to create your own (record) type and use operators to convert from your format to a real float (and back again).

Eugene Loza

  • Hero Member
  • *****
  • Posts: 663
    • My games in Pascal
Re: Accountancy format of 'Floats'
« Reply #2 on: January 10, 2017, 06:29:21 pm »
Maybe, trying StringReplace would do nice and easy?
I.e. (pseudocode)
Code: Pascal  [Select][+][-]
  1. function MyStrToFloat(inp: string): double;
  2. var s: string;
  3. begin
  4.   if pos('(',inp)>0 then begin
  5.     s := inp;
  6.     stringReplace(s,'(','',[rfReplaceAll]);
  7.     stringReplace(s,')','',[rfReplaceAll]);
  8.     result := - StrToFloat(s);
  9.   end
  10.   else
  11.     result := StrToFloat(inp);
  12. end;
Or a little more simple but less input error-proof :
Code: Pascal  [Select][+][-]
  1. function MyStrToFloat(inp: string): double;
  2. var s: string;
  3. begin
  4.   s := trim(inp)
  5.   if copy(inp,1,1)='(' then
  6.     result := - StrToFloat(copy(s,2,length(s)-2));
  7.   else
  8.     result := StrToFloat(s);
  9. end;
However, I see that http://www.freepascal.org/docs-html/rtl/sysutils/formatfloat.html can also work here, thou I've never used it for the exact task.
« Last Edit: January 10, 2017, 06:33:35 pm by Eugene Loza »
My FOSS games in FreePascal&CastleGameEngine: https://decoherence.itch.io/ (Sources: https://gitlab.com/EugeneLoza)

J-G

  • Hero Member
  • *****
  • Posts: 953
Re: Accountancy format of 'Floats'
« Reply #3 on: January 10, 2017, 06:33:08 pm »
Indeed brackets around a number make it a non number by default (being it float or otherwise).

Try to look at it from the other end, namely the component that displays the value. What are you currently using for that ?
Indeed Molly - turn it on it's head  :)    - - -   I'm using a TEdit

If those are simple numeric display components then you might be able to tweak those to display the number(s) as you'd like them to be displayed.
Your phrase 'simple numeric display components' makes me think that there ARE edit components that don't use strings, but numbers, as the displayable property (ie, .text or .caption) but I've yet to discover any. (I have only been using Laz since mid August as you know  :) )

I do know about the 'NumbersOnly' property of TEdit but that doesn't work for me since I also consider '.', '-','+' and ' ' to be valid in numeric fields; and that property doesn't. In any case it still needs a string to display.

In the case of your floattostr example, you can replace that with your own function, make the negative check and return the value bracketed. The opposite can also be done in case you wish to stay consistent.
That is my 'work-around';

Easier would be to create your own (record) type and use operators to convert from your format to a real float (and back again).
That's an interesting possibility - - -  not that I have any real idea as to how to go about it!

FPC 3.0.0 - Lazarus 1.6 &
FPC 3.2.2  - Lazarus 2.2.0 
Win 7 Ult 64

Bart

  • Hero Member
  • *****
  • Posts: 5275
    • Bart en Mariska's Webstek
Re: Accountancy format of 'Floats'
« Reply #4 on: January 10, 2017, 06:36:10 pm »
Your phrase 'simple numeric display components' makes me think that there ARE edit components that don't use strings, but numbers, as the displayable property (ie, .text or .caption) but I've yet to discover any. (I have only been using Laz since mid August as you know  :) )

TFloatSpinEdit or TFloatSpinEditEx.

Bart

derek.john.evans

  • Guest
Re: Accountancy format of 'Floats'
« Reply #5 on: January 10, 2017, 06:47:28 pm »
http://www.delphibasics.co.uk/RTL.asp?Name=NegCurrFormat

Code: Pascal  [Select][+][-]
  1.   DefaultFormatSettings.NegCurrFormat := 0;
  2.  
  3.   Format('%m', [-100.0]);
  4.  
  5.   FloatToStrF(-100.0, ffCurrency, 15, 0);    
  6.  

It doesn't work with FloatToStr() because the FloatFormat is set to ffGeneral:

From sysutils:
Code: Pascal  [Select][+][-]
  1. Function FloatToStr(Value: Currency; Const FormatSettings: TFormatSettings): String;
  2. Begin
  3.   Result := FloatToStrFIntl(Value, ffGeneral, 15, 0, fvCurrency,FormatSettings);
  4. End;  
  5.  

Unsure if this is a bug since I haven't got Delphi to test the "standard" behavior.
« Last Edit: January 10, 2017, 06:52:08 pm by Geepster »

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Accountancy format of 'Floats'
« Reply #6 on: January 10, 2017, 07:15:16 pm »
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).

Quote
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).

Quote
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).

Quote
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:
Code: Pascal  [Select][+][-]
  1. var
  2.   x : TAccountancyNumber;
  3. begin
  4.   WriteLn('x := ', String(x));
  5. end.
  6.  
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).

Code: Pascal  [Select][+][-]
  1. program test;
  2.  
  3. {$MODE OBJFPC}{$H+}
  4. {$MODESWITCh ADVANCEDRECORDS}
  5.  
  6. Uses
  7.   SysUtils, StrUtils;
  8.  
  9. Type
  10.   TAccountancyNumber = record
  11.     class operator := (source: Double): TAccountancyNumber;
  12.     class operator := (source: TAccountancyNumber): String;
  13.     class operator := (source: String): TAccountancyNumber;
  14.    private
  15.     Number : Double;
  16.    public
  17.   end;
  18.  
  19. class operator TAccountancyNumber.:= (source: Double): TAccountancyNumber;
  20. begin
  21.   Result.Number := Source;
  22. end;
  23.  
  24. class operator TAccountancyNumber.:= (source: TAccountancyNumber): String;
  25. begin
  26.   if (Source.number < 0.0)
  27.   then Result := '(' + FloatToStr(Source.number) + ')'
  28.   else Result :=       FloatToStr(Source.number);
  29. end;
  30.  
  31. class operator TAccountancyNumber.:= (source: string): TAccountancyNumber;
  32. begin
  33.   if ( (LeftStr(source,1) = '(') and (RightStr(source,1) = ')') )
  34.   then Result.Number := -StrToFloat(TrimSet(source, ['(',')'] ) )
  35.   else Result.Number :=  StrToFloat(source)
  36. end;
  37.  
  38. var
  39.   k : double;
  40.   l : double;
  41.  
  42.   x : TAccountancyNumber;
  43.   y : TAccountancyNumber;
  44. begin
  45.   k := -25.00;
  46.   l := 30.00;
  47.   x := -15.00;
  48.   y := 138.00;
  49.   WriteLn('k := ', k);
  50.   WriteLn('l := ', l);
  51.   WriteLn('x := ', String(x));
  52.   WriteLn('y := ', String(y));
  53.   x := '(40)';
  54.   WriteLn('x := ', String(x));
  55. end.
  56.  
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
« Last Edit: January 10, 2017, 07:40:54 pm by molly »

J-G

  • Hero Member
  • *****
  • Posts: 953
Re: Accountancy format of 'Floats'
« Reply #7 on: January 10, 2017, 07:23:56 pm »
Pretty much how I've done the work-around but you've taught me some new tricks Eugene, thanks
Code: Pascal  [Select][+][-]
  1.   if pos('(',inp)>0 then begin
I'm still using 
Code: Pascal  [Select][+][-]
  1. p := pos('(',inp);
  2. if p > 0 then  begin
which I remember from my TP days
Quote from: Eugene Loza
Or a little more simple but less input error-proof :
I don't have a problem with input checking, it would normally be '-20' rather than '(20)' and in fact that last would display an error. Once checked the display would change to £(20.00)
I did consider allowing '(20)' but that is 66% more work than '-20' and ease of use is more important than covering all potential input formats.
Quote from: Eugene Loza
However, I see that http://www.freepascal.org/docs-html/rtl/sysutils/formatfloat.html can also work here, thou I've never used it for the exact task.
That looks as if it would be OK but doesn't address the return to a value; which is where my issue really is.
FPC 3.0.0 - Lazarus 1.6 &
FPC 3.2.2  - Lazarus 2.2.0 
Win 7 Ult 64

J-G

  • Hero Member
  • *****
  • Posts: 953
Re: Accountancy format of 'Floats'
« Reply #8 on: January 10, 2017, 07:33:02 pm »
Your phrase 'simple numeric display components' makes me think that there ARE edit components that don't use strings, but numbers, as the displayable property

TFloatSpinEdit TFloatSpinEditEx.

Bart

Ah  ---  I see that   TFloatSpinEdit   has 'Value' as a property but there is no way to not display the up/dn arrows which would look 'ugly' on an invoice form.

I can't find a component  TFloatSpinEditEx  to view the properties but I anticipate that the same issue would be present.
FPC 3.0.0 - Lazarus 1.6 &
FPC 3.2.2  - Lazarus 2.2.0 
Win 7 Ult 64

J-G

  • Hero Member
  • *****
  • Posts: 953
Re: Accountancy format of 'Floats'
« Reply #9 on: January 10, 2017, 07:52:15 pm »
@ Molly
As usual a very fulsome reply thanks and some very useful pointers in there.

Perhaps my other replies (the forum responses are coming thick and fast - faster than I can evaluate and respond !! )  will give you a better insight as to what I'm attempting to achieve.

The point I was making about only using Laz since August was just that I haven't had time to evaluate all potential components - I'm working at it  :D

Your code regarding creating class operators is certainly interesting and does give me food for thought. It's way beyond my current knowledge but at one time so was printing - and I cracked that in a few hours!

I did look at TMaskEdit but again that looks 'ugly' on the screen with underlines and I don't really have a problem with input checking anyway, just recovering a value from a 'prettily' formatted TEdit.text  -  and that is only because I allow the freedom to input changes at any time and the conversions take place 'OnEditingDone'.

« Last Edit: January 10, 2017, 07:55:17 pm by J-G »
FPC 3.0.0 - Lazarus 1.6 &
FPC 3.2.2  - Lazarus 2.2.0 
Win 7 Ult 64

bylaardt

  • Sr. Member
  • ****
  • Posts: 309
Re: Accountancy format of 'Floats'
« Reply #10 on: January 10, 2017, 08:53:06 pm »
I am an accountant here in Brazil and and i prefer entry accounting data with two differents Tedits. It's reduce entry errors when doesn't need to input negative values.

Rethink the possibility of creating separate entry fields between debit and credit without negative entrys.

And the output formatting can be done by the difference of this fields.

J-G

  • Hero Member
  • *****
  • Posts: 953
Re: Accountancy format of 'Floats'
« Reply #11 on: January 10, 2017, 09:41:59 pm »
I am an accountant here in Brazil and and i prefer entry accounting data with two differents Tedits. It's reduce entry errors when doesn't need to input negative values.

Rethink the possibility of creating separate entry fields between debit and credit without negative entrys.

And the output formatting can be done by the difference of this fields.

Your logic is very sound bylaardt and I would generally agree with you were I writing a program to be used in general commerce. In that case there certainly should be separate routines for Invoices and Credit Notes  -  and nere the twain should meet - but in this instance it's just for my own company use and I doubt that I'll raise more than 20 Invoices during a fiscal year. (I'm semi-retired).   It just happens that I made an error on an Invoice for a customer last year and only discovered it when they placed a new order yesterday. So, rather than spend time creating a 'Credit Note' process, I simply added a Credit Item with negative value to the new Invoice.

It was easy to show the value on the printed Invoice as £(20.00) since output to a printer is string anyway but I also wanted the screen to look 'right' - (in my opinion  :) ) - and that started my steep decent into negative float formatting !
FPC 3.0.0 - Lazarus 1.6 &
FPC 3.2.2  - Lazarus 2.2.0 
Win 7 Ult 64

GAN

  • Sr. Member
  • ****
  • Posts: 370
Re: Accountancy format of 'Floats'
« Reply #12 on: January 10, 2017, 10:43:43 pm »
I am an accountant here in Brazil and and i prefer entry accounting data with two differents Tedits. It's reduce entry errors when doesn't need to input negative values.

Rethink the possibility of creating separate entry fields between debit and credit without negative entrys.

And the output formatting can be done by the difference of this fields.

I do the same ;D

J-G I use jujiboutils package and could help you because have a value and format properties, take a look http://wiki.lazarus.freepascal.org/jujiboutils
Lazarus 2.0.8 FPC 3.0.4 Linux Mint Mate 19.3
Zeos 7̶.̶2̶.̶6̶ 7.1.3a-stable - Sqlite 3.32.3 - LazReport

J-G

  • Hero Member
  • *****
  • Posts: 953
Re: Accountancy format of 'Floats'
« Reply #13 on: January 11, 2017, 12:42:01 am »
J-G I use jujiboutils package and could help you because have a value and format properties
I already have the jujiboutils package and have looked at the offerings there but all the FloatEdit components have a label which can't be hidden so totally useless for this application.
FPC 3.0.0 - Lazarus 1.6 &
FPC 3.2.2  - Lazarus 2.2.0 
Win 7 Ult 64

GAN

  • Sr. Member
  • ****
  • Posts: 370
Re: Accountancy format of 'Floats'
« Reply #14 on: January 11, 2017, 12:54:47 am »
J-G I use jujiboutils package and could help you because have a value and format properties
I already have the jujiboutils package and have looked at the offerings there but all the FloatEdit components have a label which can't be hidden so totally useless for this application.

To hide de label:

Code: Pascal  [Select][+][-]
  1. JLabeledFloatEdit1.EditLabel.Caption:='';

You can see the label in the form on desing time but when you run the program the label is hidden.
Lazarus 2.0.8 FPC 3.0.4 Linux Mint Mate 19.3
Zeos 7̶.̶2̶.̶6̶ 7.1.3a-stable - Sqlite 3.32.3 - LazReport

 

TinyPortal © 2005-2018