Avatar of cebu1014
cebu1014

asked on 

Format a decimal to string

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) + "+"
FoxPro

Avatar of undefined
Last Comment
jrbbldr
Avatar of jrbbldr
jrbbldr

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
SOLUTION
Avatar of JF0
JF0
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of cebu1014
cebu1014

ASKER

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.
ASKER CERTIFIED SOLUTION
Avatar of Pavel Celba
Pavel Celba
Flag of Czechia image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
SOLUTION
Avatar of jrbbldr
jrbbldr

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of JF0
JF0
Flag of United States of America image

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.
Avatar of Pavel Celba
Pavel Celba
Flag of Czechia image

If  ALLTRIM(b->promodsc) was working as stated in the question then b->promodsc must be character.

Avatar of jrbbldr
jrbbldr

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
Avatar of jrbbldr
jrbbldr

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
FoxPro
FoxPro

Visual FoxPro (VFP), and its predecessor FoxPro, is a data-centric, object-oriented, procedural, database programming language and IDE from Microsoft last released in 2007 that still has some active use due to its low cost of deployment and fairly rapid development. In 2008, Microsoft released a set of add-ons for VFP's xBase components to allow interoperability with various Microsoft technologies. It allows data processing against its native file-based data tables or database servers such as SQL Server.

11K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo