Format a decimal to string

cebu1014
cebu1014 used Ask the Experts™
on
I didnt develop this code. I want to modify the last line.
The promodsc will be a negative number with decimal value. I want to eliminate the sign and decimal. For example promodsc is -4.99. I want it change to 499. The rest of command will give me 0000000499+ string I need. What do I need to change here?

REPLACE TAX_AMT   WITH RIGHT("0000000000" + ALLTRIM(B->TAXTOTAL),10) + "+"
   REPLACE FRT       WITH RIGHT("0000000000" + ALLTRIM(B->SHIPTOTAL),10) + "+"
   REPLACE BPAY_TYP  WITH "04"
   REPLACE DISC_AMT  WITH RIGHT("0000000000" + ALLTRIM(b->promodsc),10) + "+"
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
The VFP Function STR() will convert a Numeric to a String.

If you look in your VFP Help for that funcion you will see it described in detail.  

Another VFP function which may work for you is the TRANSFORM() function.  
Again look into your VFP Help for more explanation

Lastly if you want to Pad out the string value to a fixed number of digits you will likely want to use the PADL() or PADR() function (see your VFP Help)

Note that since you seem to want to change the numeric value (example:  from -4.99 to 499) I'd recommend doing that (maybe in a temporary cursor) PRIOR to converting the value to a String.

Perhaps something like:
    nTestValue = -4.99
    nNewTestValue = ((-1 * nTestValue) * 100)
    cNewTestValue = ALLTRIM(STR(nNewTestValue))
    cNewTestValue = PADL(cNewTestValue,10,'0')
If you have questions about any individual line, look up the function in your VFP Help.

Good Luck
JF0
Commented:
REPLACE DISC_AMT  WITH PADL(ALLTRIM(STRTRAN(STR(ABS(promodsc),99,2),".")),10,"0")

Author

Commented:
I use this command.
  REPLACE DISC_AMT  WITH PADL(ALLTRIM(STRTRAN(STR(ABS(b->promodsc),99,2),".")),10,"0")

Resulting error message..
Function argument value,type or count is invalid.
Angular Fundamentals

Learn the fundamentals of Angular 2, a JavaScript framework for developing dynamic single page applications.

You may try:

REPLACE DISC_AMT  WITH RIGHT("0000000000" + CHRTRAN(b->promodsc,"-. ",""),10) + "+"
Commented:
PADL(ALLTRIM(STRTRAN(STR(ABS(b->promodsc),99,2),".")),10,"0")
Resulting error message..
Function argument value,type or count is invalid.


In order to determine which part of your command is causing the error message, break it down into is pieces and review the results.

?ABS(b.promodsc)
?STR(ABS(b->promodsc),99,2)
* --- I think your problem is here since I don't see the 2nd 'change to' parameter above ---
?STRTRAN(STR(ABS(b->promodsc),99,2),".","")  

? ALLTRIM(STRTRAN(STR(ABS(b->promodsc),99,2),".","") )
?PADL(ALLTRIM(STRTRAN(STR(ABS(b->promodsc),99,2),".","") ),10,"0")

Good Luck

JF0

Commented:
Probably the problem is that your data type is character and I assumed it was numeric. Try this:

PADL(ALLTRIM(STRTRAN(STR(ABS(VAL(b->promodsc)),99,2),".")),10,"0")

jrbbldr: omitting the 2nd parameter is quite alright.
If  ALLTRIM(b->promodsc) was working as stated in the question then b->promodsc must be character.

Commented:
If indeed the value of   b.promodsc   was a character instead of a numeric (as suggested above by: example promodsc is -4.99)  then by breaking the complex code down to its most simple components in a step-by-step manner would show you specifically where the problem existed.

For example if you should try the following on a Character value, you will get an error message which is telling you that the value cannot be run within the ABS() function.
?ABS(b.promodsc)

Good Luck

Commented:
If the field type of    b.promodsc   is character, then you obviously do not need to do the Functions which relate to Numeric values.

Instead you can get rid of the minus sign (a character) merely by using STRTRAN() or CHRTRAN()
And you can also get rid of the decimal point (a character) by using STRTRAN() or CHRTRAN()

Good Luck

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial