0

Coming from .NET, I'm trying to understand the following.

DEFINE total_real DECIMAL(12,5)
DEFINE total_dec  DECIMAL(6,5)
DEFINE total_int  SMALLINT

LET total_real = (total_real / 50)      
LET total_int = total_real
LET total_dec = total_real - total_int

What is the purpose of total_real - total_int?

I tried to do some similar with C# but can't do it without first converting total_int to double/decimal. Is this just converting a decimal to int?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
causita
  • 1,607
  • 1
  • 20
  • 32

2 Answers2

1

You should read the manual on the 4GL!

It appears that the code is taking a "real number", dividing it by 50, and then getting the 'whole number' and 'decimal fraction' parts.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Richard Schneider
  • 34,944
  • 9
  • 57
  • 73
1

The code is written dangerously, but as Richard Scheider surmised in his answer, the intent is to get the integer and fractional parts of the number.

The problem is that SMALLINT is a 16-bit quantity with a range of ±32,767 (-32,768 is reserved to represent NULL). And a DECIMAL(12,5) can accept values up to 9,999,999.99999 in magnitude. And that number divided by 50 is a lot bigger than the range of values that fits in a SMALLINT.

Doing the job in 'all decimal', you might use:

DEFINE total_real DECIMAL(12,5)
DEFINE total_dec  DECIMAL(6,5)
DEFINE total_frac DECIMAL(6,0)

LET total_real = (total_real / 50)      
LET total_int  = TRUNC(total_real)
LET total_frac = total_real - total_int

Alternatively, simply change SMALLINT to INTEGER. INTEGER is a 32-bit signed quantity.

Community
  • 1
  • 1
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278