Important: The information in this document is obsolete and should not be used for new development.
dec2str
You can use thedec2strfunction to convert a number in adecimalstructure to a decimal string.
void dec2str (const decform *f, const decimal *d, char *s);
f- A
decformstructure that describes how the number should look in decimal. See page 9-14 for a description of thedecformstructure.d- The
decimalstructure to be converted. See page 9-13 for the definition of thedecimalstructure.s- On return, a string representing the number in decimal.
DESCRIPTION
Thedec2strfunction is the PowerPC Numerics formatter. It takes a number from a decimal structure and converts it to a string. You can use thenum2decfunction to convert a binary floating-point number to adecimalstructure appropriate for input to thedec2strfunction.
The numeric formatter is controlled by a
- IMPORTANT
- Use the same decimal format structure settings for
dec2stras you used fornum2dec; otherwise, results are unspecified.![]()
decformstructuref. With floating style, numbers formatted using the same value forf.digitshave aligning decimal points and e's. To ensure that numbers have the same width also, pad the exponent-digits field with spaces to a width of 4. For example, iff.digits= 12, then pad 12 + 8 - length(s) spaces on the right of the result strings. The value 8 accounts for the sign, point, letter e, exponent sign, and four exponent digits. Note that this scheme gives the correct field width for NaNs and Infinities too.With fixed style, numbers formatted using the same value for
f.digitshave aligning decimal points if enough leading spaces are added to the result stringsto attain a fixed width, which must be no narrower than the widests.
- IMPORTANT
- When you create a
decimalstructure, you must setsig.lengthto the size of the string you place insig.text. You cannot leave thelengthfield undefined.![]()
EXCEPTIONS
The formatter is always exact and signals no exceptions.SPECIAL CASES
For fixed-point formatting,dec2strtreats a negative value fordigitsas a specification for rounding to the left of the decimal point; for example,digits= -2 means to round to hundreds. For floating-point formatting, values fordigitsless than 1 are treated as 1.NaNs are formatted as
NAN; Infinities are formatted asINF. A leading sign or space is included according to the style convention.The formatter never returns fewer significant digits than are contained in
sig. However, if thedecformstructure calls for more significant digits than are contained insig, then the formatter pads with zeros as needed.If more than 80 characters are required to honor
digits, then the formatter returns the string "?".EXAMPLES
Suppose you have an accounting program that computes exact values using binary numbers of pennies and prints outputs in dollars and cents. If you simply divide
the number of pennies by 100 to get dollars, you incur errors because hundredths are not exact in binary. One way to print out exact values in dollars and cents is to convert the number of pennies to adecimalstructure, perform the division by adjusting the exponent, and print the result, as shown in Listing 9-1.Listing 9-1 Accounting program
#include <fp.h> decform df; double pennies; /* This is the input value */ decimal dpennies; /* decimal value for pennies */ char * dollars; /* string to print as $$$. */ { df.style = FIXEDDECIMAL; df.digits = 0; /* start with 0 digits after decimal point */ num2dec(&df, pennies, &dpennies); /* decimal pennies */ dpennies.exp = dpennies.exp - 2; /* divide by 100 */ df.digits = 2; /* request 2 digits after decimal point */ dec2str(&df, &dpennies, dollars); /* dollar string to print */ }