How do I find out the length of a decimal field

vexiteer

New Member
Hello,

Anyone out there know how to find the lenght of a decimal field.

I tried this:
DEFINE VAR ckamount AS DECIMAL FORMAT ">>>>>>>>>9.99":U.
DEFINE VAR blklen AS INT.

ASSIGN blklen = LENGTH(ckamount)

I get the error message: Incompatiable datatypes in expression or assigment.

Later in my code I need to put leading zeros in front of so there are no empty spaces chamount example checkamount 10452.09. I need to output it as (12 spaces I need to fill up right justified zero filled in data format 9(9)V99). 000001045209. I know your thinking what the heck is 9(9)V99) data format that means two decimal places implied. Usually I can figure these things out on my own. This one has me stumped.

filler is a variable that I am going to assign once I figure out the lenght of ckamount.
PUT UNFORMATTED FILL(zero,filler)ckamount.

Is there an easy way to do this? Treading in 4GL.

Thank you all for reading,

Steve
 
/* length is for string, raw and blob data types */


DEFINE VAR ckamount AS DECIMAL FORMAT ">>>>>>>>>9.99":U.

DEFINE VARIABLE CheckAmount AS CHARACTER NO-UNDO.
ckamount = 10452.09.
ASSIGN
CheckAmount = STRING(ckamount)
CheckAmount = REPLACE(CheckAmount,"." , "")
CheckAmount = FILL("0",12 - LENGTH(CheckAmount)) + CheckAmount.

MESSAGE CheckAmount.
 

andre42

Member
Hi Steve,
hi Cecil,

I'd prefer the following solution, although Cecils code delivers the same result in most cases. It is not necessary to calculate the length yourself.
I left out the format option in the variable definition since it is not used in this context anyway.

Code:
DEFINE VAR ckamount AS DECIMAL NO-UNDO.

DEFINE VARIABLE CheckAmount AS CHARACTER NO-UNDO.
ckamount = 10452.09.
ASSIGN
  CheckAmount = STRING(ckamount, '9999999999.99':U)
  CheckAmount = REPLACE(CheckAmount,"." , "")
  .

MESSAGE CheckAmount.
When not using a format with string(), beware in case ckamount does not have exactly two decimals!

Regards,

André
 
Hi Steve and Andre,

Andre's example will work better as it will raise an error if the input string is longer than 12 characters. My example would pass it with extra characters. I was trying to highlight that length function needed to be used on a character string.
 

vexiteer

New Member
Thanks Guy before I could write about the issue the decimal point. Andre posted his solution. Thanks a bunch.
 

TomBascom

Curmudgeon
Hello,

Anyone out there know how to find the lenght of a decimal field.

I tried this:
DEFINE VAR ckamount AS DECIMAL FORMAT ">>>>>>>>>9.99":U.

... I need to put leading zeros in front of so there are no empty spaces chamount example checkamount 10452.09. I need to output it as (12 spaces I need to fill up right justified zero filled in data format 9(9)V99). 000001045209. I know your thinking what the heck is 9(9)V99) data format that means two decimal places implied. Usually I can figure these things out on my own. This one has me stumped.

Maybe I'm missing something but why wouldn't:

Code:
DEFINE VAR ckAmount AS DECIMAL FORMAT "9999999999.99":U.

display ckAmount.

Do what you describe needs to be done?
 

andre42

Member
Hi Tom,

Maybe I'm missing something but why wouldn't:

Code:
DEFINE VAR ckAmount AS DECIMAL FORMAT "9999999999.99":U.

display ckAmount.
Do what you describe needs to be done?

As far as I understood it Steve wants to obmit the decimal point. I can't think of any solution without explicit conversion and processing the resulting string.
 

StuartT

Member
or you could simply take the 2 decimal places number, multiply by 100 then string it and take the length of the string :)

DEFINE VAR ckamount AS DECIMAL FORMAT ">>>>>>>>>9.99":U.
define var intval as int.
define var charval as char.

DEFINE VAR blklen AS INT.

assign intval = ckamount * 100.
assign charval = string(intval).
ASSIGN blklen = LENGTH(charval).
 

TomBascom

Curmudgeon
Hi Tom,
As far as I understood it Steve wants to obmit the decimal point. I can't think of any solution without explicit conversion and processing the resulting string.

Ah, that's what I missed. Then:

Code:
display ( ckAmount * 100 ) format "999999999999".

should do the trick.
 

TomBascom

Curmudgeon
Yes. But, as I read the question, the reason for wanting to know the length is to left pad it with zeros and eliminate the decimal.
 
Top