Thread: How to convert float to string without using sprintf()

  1. #1
    Registered User
    Join Date
    Nov 2008
    Location
    China
    Posts
    17

    How to convert float to string without using sprintf()

    EX:
    float a = 88.88888;
    char *s = "88.88888"

    ps: how to write float into binary file and how to read it?

  2. #2
    Registered User
    Join Date
    Oct 2010
    Posts
    107
    Why do you want to avoid sprintf? It's the preferred solution according to my cursory looks at Google. Anyway's here's the other way:

    Code:
    ECVT(3)                    Linux Programmer's Manual                   ECVT(3)
    
    NAME
           ecvt, fcvt - convert a floating-point number to a string
    
    SYNOPSIS
           #include <stdlib.h>
    
           char *ecvt(double number, int ndigits, int *decpt, int *sign);
    
           char *fcvt(double number, int ndigits, int *decpt, int *sign);
    
       Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
    
           ecvt(), fcvt(): _SVID_SOURCE || _XOPEN_SOURCE >= 500
    
    DESCRIPTION
           The  ecvt()  function  converts  number  to a null-terminated string of
           ndigits digits (where ndigits is reduced  to  a  system-specific  limit
           determined  by the precision of a double), and returns a pointer to the
           string.  The high-order digit is nonzero, unless number is  zero.   The
           low order digit is rounded.  The string itself does not contain a deci‐
           mal point; however, the position of the decimal point relative  to  the
           start  of  the string is stored in *decpt.  A negative value for *decpt
           means that the decimal point is to the left of the start of the string.
           If  the  sign  of  number is negative, *sign is set to a nonzero value,
           otherwise it is set to 0.  If number is zero, it is unspecified whether
           *decpt is 0 or 1.
    
           The  fcvt() function is identical to ecvt(), except that ndigits speci‐
           fies the number of digits after the decimal point.
    
    RETURN VALUE
           Both the ecvt() and fcvt() functions  return  a  pointer  to  a  static
           string  containing  the  ASCII  representation  of  number.  The static
           string is overwritten by each call to ecvt() or fcvt().
    Clearly these functions are harder to use, which seems to be why sprintf is preferred.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > ps: how to write float into binary file and how to read it?
    fwrite() and fread()
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Aug 2010
    Posts
    231
    Quote Originally Posted by liyanhong View Post
    EX:
    float a = 88.88888;
    char *s = "88.88888"
    Try strtod (C89).

  5. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    As Salem suggested, why are you converting the value to a string if you're writing to a binary file? Just write the value directly using fwrite().
    If you understand what you're doing, you're not learning anything.

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by QuadraticFighte View Post
    Clearly these functions are harder to use, which seems to be why sprintf is preferred.
    The more likely reason for preferring sprintf() is that "these functions" are also not part of the C standard, so are not guaranteed to be available for all compilers/libraries.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  7. #7
    Registered User
    Join Date
    Oct 2010
    Posts
    107
    Quote Originally Posted by BillyTKid View Post
    Try strtod (C89).
    The title of this post is "How to convert float to string without using sprintf()"

    The man page for strtod says "convert ASCII string to floating-point number."

    It could be that I totally forgot how to read, but that seems like the precise opposite of what the OP asked for.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  2. Moving Average Question
    By GCNDoug in forum C Programming
    Replies: 4
    Last Post: 04-23-2007, 11:05 PM
  3. Debug Error Really Quick Question
    By GCNDoug in forum C Programming
    Replies: 1
    Last Post: 04-23-2007, 12:05 PM
  4. Opengl walking leg animation
    By Bobby230 in forum C Programming
    Replies: 3
    Last Post: 03-05-2006, 03:41 PM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM