floats and strings

This is a discussion on floats and strings within the C Programming forums, part of the General Programming Boards category; Does anyone know of a way to convert a float into a string? e.g. float 1.23; BECOMES char Array[4] = ...

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    4

    Lightbulb floats and strings

    Does anyone know of a way to convert a float into a string?

    e.g.

    float 1.23; BECOMES char Array[4] = "1.23";

  2. #2
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    Something like -

    char Array[5];
    float a = 1.23F;
    sprintf(Array,"%g",a);
    zen

  3. #3
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    itoa() is a non-standard function. sprintf() is standard and if you know how to use printf() then it is easy enough to use.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  4. #4
    Registered User
    Join Date
    Nov 2001
    Posts
    4
    Thanks Guys,

    My stdlib.h dosen't contain ltoa, ftoa, itoa etc. which is why I had the problem.

    sprintf() works fine.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A good way to convert strings to floats?
    By jcafaro10 in forum C++ Programming
    Replies: 4
    Last Post: 01-20-2009, 11:08 PM
  2. Need help with using strings in classes
    By orikon in forum C++ Programming
    Replies: 5
    Last Post: 11-12-2005, 02:01 PM
  3. file saving: trouble with strings and strncpy()
    By psychopath in forum C++ Programming
    Replies: 10
    Last Post: 09-17-2005, 10:26 PM
  4. Array of Strings
    By mjpars in forum C Programming
    Replies: 8
    Last Post: 08-21-2003, 11:15 PM
  5. strings to floats
    By bob2509 in forum C++ Programming
    Replies: 2
    Last Post: 04-20-2002, 01:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21