Thread: convert numbers to strings

  1. #1
    Unregistered
    Guest

    convert numbers to strings

    is there a way to convert numbers to strings ?
    the number could be integer/float ..

    www.akilla.tk

  2. #2
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,263
    for int

    char *_itoa( int value, char *string, int radix );

    for float

    char *_fcvt( double value, int count, int *dec, int *sign )

    look um up.
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

  3. #3
    Registered User quagsire's Avatar
    Join Date
    Jun 2002
    Posts
    60
    Code:
    char word[20];
    int number1 = 12345;
    float number2 = 678.9;
    sprintf(word, "%d  %f", number1, number2);
    cout << word;
    The format specifiers are the same as for printf.

  4. #4
    Unregistered
    Guest

    Unhappy but...

    the output is
    12345 678.900024

    here's my code...
    PHP Code:
    #include <stdio.h>
    #include <iostream.h>

    main()
    {
        
    char word[20];
        
    int number1 12345;
        
    float number2 678.9;
        
    sprintf(word"%d  %f"number1number2);
        
    cout << word;
        
    cin.get();

    COOL PROGRAMS @ www.akilla.tk

  5. #5
    Unregistered
    Guest

    Question Where should I refer ?

    for int

    char *_itoa( int value, char *string, int radix );

    for float

    char *_fcvt( double value, int count, int *dec, int *sign )

    look um up.
    By the way, how do you know about these functions ??
    Is there a book that tells all these things that I can refer
    to ? Is it online ?

    COOL PROGRAMS @ www.akilla.tk

  6. #6
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680

    Re: but...

    Originally posted by Unregistered
    the output is
    12345 678.900024
    That's the disadvantage of using floats.
    Your program is good (you could try: %.1f) but the OS can't handle floats very well.

    Originally posted by Unregistered
    By the way, how do you know about these functions ??
    Is there a book that tells all these things that I can refer
    to ? Is it online ?
    You can take a look in the Microsoft MSDN library for example: _itoa

  7. #7
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,263
    >
    By the way, how do you know about these functions ??
    Is there a book that tells all these things that I can refer
    to ?
    <

    butt loads of questions, and research such as reading my headers, and the MSDN as Monster posted, it defines and explains virtually every function in standard C/C++ and many other languages.

    it is Microsofts single greatest accomplishment.

    >Is it online ?

    yeah,

    http://msdn.microsoft.com
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

  8. #8
    Registered User
    Join Date
    Jun 2002
    Posts
    82
    I like this one as well:

    Code:
    #include <sstream>
    using namespace std;
    int main() {
        ostringstream oss, oss2;
        oss << 12345;
        oss2 << 678.9;
        string s1 = oss1.str(), s2 = oss2.str();
        cout << s1 << endl << s2 << endl;
    }
    On my machine (Solaris CC compiler) I don't have that pesky formatting problem.
    Claus Hetzer
    Compiler: Borland 5.5 (on Windows)
    Solaris CC (on Unix)
    Known Languages: C++, MATLAB, Perl, Java

  9. #9
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Perhaps you could write an algorithm to do it? It is not as difficult as you may think...
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Working with Strings and Numbers
    By jamez05 in forum C++ Programming
    Replies: 8
    Last Post: 10-19-2005, 12:39 PM
  2. Convert Phone Letters To Numbers
    By nicolobj in forum C Programming
    Replies: 4
    Last Post: 10-03-2002, 03:53 PM
  3. strings and numbers
    By Granger9 in forum C Programming
    Replies: 3
    Last Post: 09-10-2002, 04:11 PM
  4. A (complex) question on numbers
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 02-03-2002, 06:38 PM
  5. Replies: 13
    Last Post: 01-22-2002, 04:38 AM