Efficient way to transform float to string

Kaleb Keithley kaleb at thyme.jpl.nasa.gov
Tue Dec 11 03:59:08 AEST 1990


Stepping into the middle of the thread.

On Sun; there is econvert and related functions.

     econvert() converts the value to a null-terminated string of
     ndigit  ASCII  digits  in  buf and returns a pointer to buf.
     buf should contain at least ndigit+1 characters.  The  posi-
     tion of the radix character relative to the beginning of the
     string is stored indirectly  through  decpt.   Thus  buf  ==
     "314"  and  *decpt  ==  1 corresponds to the numerical value
     3.14, while buf == "314" and *decpt == -1 corresponds to the
     numerical  value  .0314.  If the sign of the result is nega-
     tive, the word pointed to by sign is nonzero;  otherwise  it
     is zero.  The least significant digit is rounded.

     fconvert works much like econvert, except that  the  correct
     digit  has been rounded as if for sprintf(%w.nf) output with
     n=ndigit digits to the right of the radix character.  ndigit
     can  be  negative  to  indicate  rounding to the left of the
     radix character.  The return value is a pointer to buf.  buf
     should  contain  at  least  310+max(0,ndigit)  characters to
     accomodate any double-precision value.

     gconvert() converts the value  to  a  null-terminated  ASCII
     string  in  buf  and  returns a pointer to buf.  It produces
     ndigit significant  digits  in  fixed-decimal  format,  like
     sprintf(%w.nf),  if  possible,  and  otherwise  in floating-
     decimal format, like sprintf(%w.ne); in either case  buf  is
     ready  for  printing,  with  sign  and exponent.  The result
     corresponds to that obtained by

          (void) sprintf(buf, "%w.ng", value);


-- 
Kaleb Keithley                      Jet Propulsion Labs
kaleb at thyme.jpl.nasa.gov

You can please all of the people some of the time,



More information about the Comp.lang.c mailing list