x68/fconvert.c


DEFINITIONS

This source file includes following functions.
  1. fconvert


   1  /*
   2   * PROJECT C Library, X68000 PROGRAMMING INTERFACE DEFINITION
   3   * --------------------------------------------------------------------
   4   * This file is written by the Project C Library Group,  and completely
   5   * in public domain. You can freely use, copy, modify, and redistribute
   6   * the whole contents, without this notice.
   7   * --------------------------------------------------------------------
   8   * $Id: fconvert.c,v 1.1.1.2 1999/01/20 04:59:39 matz Exp $
   9   */
  10  /* changed 1997.2.3 by K.Okabe */
  11  
  12  /* System headers */
  13  #include <stdlib.h>
  14  #include <sys/xstdlib.h>
  15  
  16  /* Functions */
  17  char *fconvert (double x, int ndigit, int *decpt, int *sign, char *buffer)
  18  {
  19      int pos, n;
  20      char *src, *dst;
  21      char string[24];
  22      int figup;
  23  
  24      /* 18桁の文字列に変換 */
  25      _dtos18 (x, decpt, sign, string);
  26  
  27      /* コピー元アドレスを設定 */
  28      src = string;
  29  
  30      /* コピー先アドレスを設定 */
  31      dst = buffer;
  32  
  33      /* 小数点位置を得る */
  34      pos = *decpt;
  35  
  36      /* 小数点位置が負なら */
  37      if (pos < 0) {
  38  
  39          /* 埋める桁数を計算 */
  40          n = min (-pos, ndigit);
  41  
  42          /* 先頭を0で埋める */
  43          while (n-- > 0)
  44              *dst++ = '0';
  45  
  46          /* 小数点位置は0になる */
  47          *decpt = 0;
  48  
  49      }
  50  
  51      /* 残りのコピー桁数 */
  52      n = ndigit + pos;
  53  
  54      /* 格納先にコピー */
  55      while (n-- > 0) {
  56  
  57          /* 足りない部分は0で埋める */
  58          if (*src == '\0') {
  59              while (n-- >= 0)
  60                  *dst++ = '0';
  61              break;
  62          }
  63  
  64          /* 変換文字列からコピー */
  65          *dst++ = *src++;
  66  
  67      }
  68  
  69      /* 丸める */
  70      *decpt += (figup = _round (buffer, dst, *src));
  71  
  72      /* 繰り上がりがあれば末尾に0を追加する */
  73      if (figup)
  74          *dst++ = '0';
  75  
  76      /* 終端に NUL を打つ */
  77      *dst = '\0';
  78  
  79      /* アドレスを返す */
  80      return buffer;
  81  }