Back to index

php5  5.3.10
num2str.c
Go to the documentation of this file.
00001 /* num2str.c: bcmath library file. */
00002 /*
00003     Copyright (C) 1991, 1992, 1993, 1994, 1997 Free Software Foundation, Inc.
00004     Copyright (C) 2000 Philip A. Nelson
00005 
00006     This library is free software; you can redistribute it and/or
00007     modify it under the terms of the GNU Lesser General Public
00008     License as published by the Free Software Foundation; either
00009     version 2 of the License, or (at your option) any later version.
00010 
00011     This library is distributed in the hope that it will be useful,
00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014     Lesser General Public License for more details.  (COPYING.LIB)
00015 
00016     You should have received a copy of the GNU Lesser General Public
00017     License along with this library; if not, write to:
00018 
00019       The Free Software Foundation, Inc.
00020       59 Temple Place, Suite 330
00021       Boston, MA 02111-1307 USA.
00022 
00023     You may contact the author by:
00024        e-mail:  philnelson@acm.org
00025       us-mail:  Philip A. Nelson
00026                 Computer Science Department, 9062
00027                 Western Washington University
00028                 Bellingham, WA 98226-9062
00029        
00030 *************************************************************************/
00031 
00032 #include <config.h>
00033 #include <stdio.h>
00034 #include <assert.h>
00035 #include <stdlib.h>
00036 #include <ctype.h>
00037 #include <stdarg.h>
00038 #include "bcmath.h"
00039 #include "private.h"
00040 
00041 /* Convert a numbers to a string.  Base 10 only.*/
00042 
00043 char
00044 *bc_num2str (num)
00045       bc_num num;
00046 {
00047   char *str, *sptr;
00048   char *nptr;
00049   int  index, signch;
00050 
00051   /* Allocate the string memory. */
00052   signch = ( num->n_sign == PLUS ? 0 : 1 );  /* Number of sign chars. */
00053   if (num->n_scale > 0)
00054     str = (char *) safe_emalloc (1, num->n_len + num->n_scale, 2 + signch);
00055   else
00056     str = (char *) safe_emalloc (1, num->n_len, 1 + signch);
00057   if (str == NULL) bc_out_of_memory();
00058 
00059   /* The negative sign if needed. */
00060   sptr = str;
00061   if (signch) *sptr++ = '-';
00062 
00063   /* Load the whole number. */
00064   nptr = num->n_value;
00065   for (index=num->n_len; index>0; index--)
00066     *sptr++ = BCD_CHAR(*nptr++);
00067 
00068   /* Now the fraction. */
00069   if (num->n_scale > 0)
00070     {
00071       *sptr++ = '.';
00072       for (index=0; index<num->n_scale; index++)
00073        *sptr++ = BCD_CHAR(*nptr++);
00074     }
00075 
00076   /* Terminate the string and return it! */
00077   *sptr = '\0';
00078   return (str);
00079 }