Microsoft KB Archive/120183

How to Modify the Default Scientific Notation Format

PSS ID Number: Q120183 Article last modified on 07-14-1995

2.00 2.5x 2.60 2.60a | 2.5x 2.60 2.60a | 2.5x 2.60a | 2.60

MS-DOS | WINDOWS | MACINTOSH | UNIX

= SUMMARY =

The default scientific notation produced under the following conditions does not leave a significant digit to the left of the decimal point. This occurs when you specify the ‘@^’ format string in the TRANSFORM function in the PICTURE or FUNCTION clause of @..GET or @..SAY or when you specify the ‘@^’ format string in the Format dialog of the Screen Builder or Report Writer.

This article describes how, by applying a user-defined function (UDF) to the number, you can format a string so that a significant digit does appear to the left of the decimal point.

= MORE INFORMATION =

The following command demonstrates the current behavior of the ‘@^’ format string. Enter it in the Command Window:

WAIT WINDOW TRANSFORM(.0206936,‘@^’)

When decimals is set to 2, FoxPro produces a window that displays the string .20693600E+0. For more information about the TRANSFORM function and the SET DECIMALS command, please review the “Microsoft FoxPro Language Reference.”

Similar behavior occurs in the Report Writer if a field on the report has a scientific format designation. For more information about formatting fields, please see the “Designing Reports and Labels” section of the “Microsoft FoxPro User’s Guide.”

Example UDF Code to Place Single Digit to the Left of Decimal Point
The following example UDF code shows you how to reformat the result of the TRANSFORM function so that a single digit is placed to the left of the decimal point. You could also apply this UDF to a field expression in a report or screen. For information about using UDFs in reports, please see the “User-Defined Functions in Reports” section in the “Microsoft FoxPro User’s Guide.”


 * This user-defined function (UDF) requires a single argument - the number
 * to be converted to scientific notation.
 * The code assumes that SET TALK is OFF. PARAMETER theNum
 * The argument must be numeric. IF TYPE(‘theNum’) != ‘N’ RETURN ENDIF

nDecs=SET(‘DECIMALS’) && current number of decimal places


 * Get the number in its incorrect exponential form:
 * An space is trimmed from the beginning of the transformed string. cExpNum=LTRIM(TRANSFORM(theNum,‘@^’)) && result is an expC
 * Get the position of ‘E’ in the string: nEPos = AT(‘E’,cExpNum)
 * Get the power: nPower=VAL(SUBSTR(cExpNum,nEPos+1))
 * Get the numeric component preceding ‘E’: cNum=SUBSTR(cExpNum,1,nEPos-1)
 * Switch the decimal point and first digit: cNum=SwapChar(cNum,1,2)
 * Respect the value of SET(‘DECIMALS’): cnum=STR(VAL(cNum),nDecs+1,nDecs)
 * Because the number is shifted left, decrement the power: nPower=nPower-1
 * Reconstruct the string into appropriate scientific notation:
 * See if a leading zero is needed to pad the exponent: cPad=IIF( ABS(nPower)<10, ‘0’,’’)
 * Grab the sign: cSign=IIF(nPower < 0, ‘-’,‘+’)

RETURN cNum + ‘E’ + cSign + cPad + LTRIM(STR(ABS(nPower)))

-


 * Swap the characters in positions pos1 and pos2: PROC SwapChar PARAMETER srcStr, pos1, pos2

PRIVATE holdChar


 * Hold onto the character in pos1: holdChar=SUBSTR(srcStr, pos1, 1)
 * Place char2 in pos1: srcStr=STUFF(srcStr, pos1, 1, SUBSTR(srcStr, pos2, 1)) srcStr=STUFF(srcStr, pos2, 1, holdChar) && stuff old char1 in pos2 RETURN srcStr

ENDPROC && SwapChar **********************************************************

How to Use the UDF Code Step by Step
Here is an example showing how to use the UDF code:

  Enter the code into a program file and save it with the name SCINOTE.PRG.   Open the CUSTOMER table located in the TUTORIAL directory.   Create a new report.   In the Detail Band, create a new Report Expression, and in the expression dialog, enter the following command: SCINOTE(lat)   Return to the Report Form, and choose the Preview item from the Report menu. If the numbers appear as asterisks in the preview, the field has not been stretched widely enough. Return to design mode and resize the field. 

= REFERENCES =

“Microsoft FoxPro Language Reference.”

Additional reference words: FoxMac FoxDos FoxWin 2.00 2.50 2.50a 2.50b 2.50c 2.60 2.60a EXPONENT EXPONENTIAL SBuilder RWriter LWriter KBCategory: kbprg kbprint kbcode KBSubcategory: FxprgGeneral ============================================================================= Copyright Microsoft Corporation 1995.