Currency with 5 decimal digits?

I need to translate the following C # code in Delphi:

decimal XYZ = decimal.Round(dataset123.XYZ, 5, MidpointRounding.AwayFromZero);

The result will be saved in the float field in the MSSQL database.

When I work with doubleDelphi, there are several problems, since it is not a fixed decimal. In addition, when you save to the ADO database, the database viewer often displays a very long number because it contains too many digits. In addition, there is a problem with the rounding method, where rounding is not always performed “from scratch”.

I want to solve the most serious problem at the moment. I need a five-digit currency, but Delphi has only a data type currencythat has 4 digits. The presence of a 5-digit value is an important requirement for this project / business process.

In some internet sources, I read what people say about this code:

var
  x: Decimal[5,3]

But this syntax is not valid for me. I am working with Delphi 2007.

Is there anything I can do to get a 5 digit fixed decimal place?

+4
source share
2 answers

Here is a sample code from David Heffernan's suggestion to show you how to get started:

unit UnitMyCurrency;

interface

uses
  System.SysUtils;

type
  TMyCurrency = record
    Value : int64;

    class operator implicit( pValue : integer ) : TMyCurrency;
    class operator implicit( pValue : single ) : TMyCurrency;
    class operator implicit( pValue : TMyCurrency ) : single;

    class operator Add( p1 : TMyCurrency; p2 : TMyCurrency ) : TMyCurrency;
    class operator Subtract( p1 : TMyCurrency; p2 : TMyCurrency ) : TMyCurrency;

    class operator NotEqual( p1 : TMyCurrency; p2 : single ) : boolean;
  const
    cFactor = 100000;
  end;

implementation

{ TMyCurrency }

class operator TMyCurrency.implicit(pValue: integer): TMyCurrency;
begin
  Result := pValue * cFactor;
end;

class operator TMyCurrency.implicit(pValue: single): TMyCurrency;
begin
  Result := round( pValue * cFactor);
end;

class operator TMyCurrency.Add(p1, p2: TMyCurrency): TMyCurrency;
begin
  Result := TMyCurrency( p1.Value + p2.Value );
end;

class operator TMyCurrency.implicit(pValue: TMyCurrency): single;
begin
  Result := pValue.Value / cFactor;
end;

class operator TMyCurrency.NotEqual(p1: TMyCurrency; p2: single): boolean;
begin
  Result := TMyCurrency( p2 ).Value <> p1.Value;
end;

class operator TMyCurrency.Subtract(p1, p2: TMyCurrency): TMyCurrency;
begin
  Result.Value := p1.Value - p2.Value;
end;

procedure Test;
var
  v1, v2, v3 : TMyCurrency;
begin
  v1 := 5.12345;
  v2 := 6.00000;
  if (v1 + v2 ) <> 11.12345  then
  begin
    raise Exception.Create('Error Message');
  end;
end;

end.

Obviously, it is not complete, but when you are done, you will have a new type that will do what you need and that you have full control.

The following shows how to use overloaded operators. This is for Seattle, but nothing has changed since 2007.

http://docwiki.embarcadero.com/RADStudio/en/Operator_Overloading_(Delphi)

+1
source

CurrencyDecimals. - :

var
  price: Double;

begin
  price:= 1234.56789;
  CurrencyDecimals := 5;
  ShowMessage('Price = '+Format('%m', [price]));
end;
-3

Source: https://habr.com/ru/post/1624408/


All Articles