Double to String Conversion Routines

There are three routines Your application can use the exponent of the value to be displayed to choice which routine: doubleToStr() or doubleToStrScientific(), is to used.

The routines follow.

doubleToStr()

// Routine to convert double to string in fixed point notation.
// This is limited to 15 fractional digits.  Type double has at most 15 significant digits.
// Be sure that 'str' is dimensioned large enough to hold the result.
// MathLib required.
//
void doubleToStr(CharPtr str, double flpNumber, Int numFractDigits)
{ double flpIP, zeros, round;
  Int i, remainder, strLen;
  Char sign = ' ';					// a ' ' or '-' will be output for more uniform display output

  if (numFractDigits < 0 || numFractDigits > 15)
    FrmAlert(ALERTID_ERROR);

  if (flpNumber < 0.0)
    { flpNumber = -flpNumber;
      sign = '-';
    }

  zeros = pow(10, numFractDigits);			// get the rounding constant
  round = 0.5/zeros;

  flpNumber = modf(flpNumber+round, &flpIP);		// get integer and fractional parts

  str[0] = sign;
  StrIToA(&str[1], (Long) flpIP);
  strLen = StrLen(str);					// put in the decimal point and terminate the string
  str[strLen] = '.';
  str[numFractDigits+strLen+1] = '\0';

  flpNumber = flpNumber * zeros;			// fractional part

  for (i=numFractDigits+strLen; i>strLen; i--)		// convert the integer part
    { remainder = fmod(flpNumber,10);
      str[i] = remainder + 0x30;
      flpNumber /= 10;
    }
}


The end of doubleToStr()


doubleToStrScientific()

// Routine to convert double to string in scientific notation.
// This is limited to 15 fractional digits.  Requires MathLib.
// Be sure that 'str' is dimensioned large enough to hold result it could require 22 characters
//
void doubleToStrScientific(CharPtr str, double flpNumber, Int numFractDigits)
{ double flpIP, adjFlpNumber, zeros, round;
  Int i, remainder, exponent, ePos;
  Char sign = ' ';					// a ' ' or '-' will be output for more uniform display output

  if (numFractDigits < 0 || numFractDigits > 15)
    FrmAlert(ALERTID_ERROR);

  if (flpNumber < 0.0)
    { flpNumber = -flpNumber;
      sign = '-';
    }

  zeros = pow(10, numFractDigits);			// get the rounding constant -- using MathLib
  round = 0.5/zeros;

  exponent = log10(flpNumber);
  adjFlpNumber = flpNumber/pow(10, exponent);
  adjFlpNumber = modf(adjFlpNumber+round, &flpIP);	// get integer and fractional parts

  str[0] = sign;
  str[1] = flpIP+'0';					// always a single digit
  str[2] = '.';
  ePos = numFractDigits+3;

  flpNumber = adjFlpNumber * zeros;			// fractional part
  for (i=numFractDigits+2; i>2; i--)			// convert the integer part
    { remainder = fmod(flpNumber,10);
      str[i] = remainder + 0x30;
      flpNumber /= 10;
    }

  str[ePos] = 'e';					// now the exponent
  StrIToA(&str[ePos+1], (Long) exponent);
}


End of doubleToStrScientific()


doubleToStr9()

// Routine to convert double to string in fixed point notation.
// This is limited to 9 fractional digits.  This limit is required because MathLib in not used.
// Be sure that 'str' is dimensioned large enough.
// MathLib not required.
//
void doubleToStr9(CharPtr str, double flpNumber, Int numFractDigits)
{ Long  longNumber;
  double flpIP, zeros, round;
  Int i, remainder, strLen;
  Char sign = ' ';					// a ' ' or '-' will be output for more uniform display output

  if (numFractDigits < 0 || numFractDigits > 9)
    FrmAlert(ALERTID_ERROR);

  if (flpNumber < 0.0)
    { flpNumber = -flpNumber;
      sign = '-';
    }

// This could also be done with an array 1.0, 10.0, 100.0 etc.
  zeros = 1.0;
  for (i=0; i<numFractDigits; i++)
     zeros *= 10;

  round = 0.5/zeros;
  flpNumber += round;
  flpIP = (Long) flpNumber;
  flpNumber = flpNumber - flpIP;

  str[0] = sign;
  StrIToA(&str[1], (Long) flpIP);
  strLen = StrLen(str);					// put in the decimal point and terminate the string
  str[strLen] = '.';
  str[numFractDigits+strLen+1] = '\0';

  longNumber = flpNumber * zeros;			// fractional part -- this is what limits to 9 digits

  for (i=numFractDigits+strLen; i>strLen; i--)	// convert the integer part
    { remainder = longNumber % 10;
      str[i] = remainder + 0x30;
      longNumber /= 10;
    }
}


The end of doubleToStr9()



Back IconTo the Bray Palm Handheld Page