convert numbers to strings

This is a discussion on convert numbers to strings within the C++ Programming forums, part of the General Programming Boards category; is there a way to convert numbers to strings ? the number could be integer/float .. www.akilla.tk...

  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,267
    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,267
    >
    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
    Posts
    5,439
    Perhaps you could write an algorithm to do it? It is not as difficult as you may think...
    Code:
    int main(void){srand(time(0));for(double l=rand(),l0=0,l00=0;;l0+=0.1){for(double l000=0;l000
    <1;l000+=.001,l+=((double)rand()/RAND_MAX)/0x64,l00+=((sin(l*0x8*atan(l0)*l000-(l0*0x8*atan
    (l)))*0.5)+0.5)){l00-=floor(l00);for(size_t l0000=0,l00000=(size_t)(0x50*(l00));l0000<l00000;++l0000
    )putchar(0x20);putchar(0x61+(int)((double)rand()/RAND_MAX*0x1a));putchar('\n');}}return 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, 05:38 PM
  5. Replies: 13
    Last Post: 01-22-2002, 03:38 AM

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