Conversion to a string

hi programmers,

Is there any way of converting an integer/float/double value int a string so that the string stores each digit of the number in each of its constituent characters??(am i clear??)

for ex.,
if i=67,
then can i have a string s[10] such that
s[0]=6,s[1]=7,s[2]=''.

when i use explicit conversion for this(say s[0]=(char)i/100), then it only stores the character equivalent of the number(i.e.s[0] becomes A) and not the number itself..

Comments

  • : hi programmers,
    :
    : Is there any way of converting an integer/float/double value int a string so that the string stores each digit of the number in each of its constituent characters??(am i clear??)
    :
    : for ex.,
    : if i=67,
    : then can i have a string s[10] such that
    : s[0]=6,s[1]=7,s[2]=''.
    :
    : when i use explicit conversion for this(say s[0]=(char)i/100), then it only stores the character equivalent of the number(i.e.s[0] becomes A) and not the number itself..
    :
    Use the sprintf function:-

    char s[3];
    sprintf(&s, "%d", i);

    Jonathan

    ###
    for(74,117,115,116){$::a.=chr};(($_.='qwertyui')&&
    (tr/yuiqwert/her anot/))for($::b);for($::c){$_.=$^X;
    /(p.{2}l)/;$_=$1}$::b=~/(..)$/;print("$::a$::b $::c hack$1.");

  • : :
    : Use the sprintf function:-
    :
    : char s[3];[red]<< this is only large enough for 2 digits. Make it bigger if the number has more digits[/red]
    : sprintf(&s, "%d", i); [red]<<< remove the '&' symbol[/red]

  • Well, the program works fine if i is an integer but if i is a float value with some decimal values, it does not work.

    [code]

    #include
    #include
    #include
    void main()
    {clrscr();
    int i=300;
    char st[10];
    sprintf(st,"%d",i);
    cout<<st;
    getch();
    }

    [/code]

    the output comes as

    [code] 300 [/code]

    but if i is float variable

    [code]


    #include<iostream.h>
    #include
    #include
    void main()
    {clrscr();
    float i=300;
    char st[10];
    sprintf(st,"%d",i);
    cout<<st;
    getch();
    }

    [/code]

    it gives output

    [code] 0 [/code]

    is there any other function avl for float and double values??


    : : :
    : : Use the sprintf function:-
    : :
    : : char s[3];[red]<< this is only large enough for 2 digits. Make it bigger if the number has more digits[/red]
    : : sprintf(&s, "%d", i); [red]<<< remove the '&' symbol[/red]
    :
    :

  • [b][red]This message was edited by stober at 2004-1-31 10:21:13[/red][/b][hr]
    : Well, the program works fine if i is an integer but if i is a float value with some decimal values, it does not work.
    :
    [blue]That's because "%d" is for int not float or double. use "%f" for float, and "%lf" for double. you can also specify the number of decimal places, for example "%.2f" will display only 2 decimal places. The default is 6 decimals.[/blue]




  • [blue]

    hey, thanks a lot. i have got my doubts cleared.

    this site is great and you people are even greater.....

    thanks again..

    [/blue]


    : [b][red]This message was edited by stober at 2004-1-31 10:21:13[/red][/b][hr]
    : : Well, the program works fine if i is an integer but if i is a float value with some decimal values, it does not work.
    : :
    : [blue]That's because "%d" is for int not float or double. use "%f" for float, and "%lf" for double. you can also specify the number of decimal places, for example "%.2f" will display only 2 decimal places. The default is 6 decimals.[/blue]
    :
    :
    :
    :
    :

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories