float ===> char umwandeln...



  • also irgendwie habe ich das Gefühl, du hast das nicht ganz verstanden...

    aber ich glaube, du willst einfach einen Zahlenwert (eben auch ein Fließkommawert) in einzelne char-Werte je Ziffer zerlegen, oder?

    MSDN schrieb:

    _gcvt
    Converts a floating-point value to a string, which it stores in a buffer.

    char *_gcvt( double value, int digits, char *buffer );

    Routine Required Header Compatibility
    _gcvt <stdlib.h> Win 95, Win NT

    For additional compatibility information, see Compatibility in the Introduction.

    Libraries

    LIBC.LIB Single thread static library, retail version
    LIBCMT.LIB Multithread static library, retail version
    MSVCRT.LIB Import library for MSVCRT.DLL, retail version

    Return Value

    _gcvt returns a pointer to the string of digits. There is no error return.

    Parameters

    value

    Value to be converted

    digits

    Number of significant digits stored

    buffer

    Storage location for result

    Remarks

    The _gcvt function converts a floating-point value to a character string (which includes a decimal point and a possible sign byte) and stores the string in buffer. The buffer should be large enough to accommodate the converted value plus a terminating null character, which is appended automatically. If a buffer size of digits + 1 is used, the function overwrites the end of the buffer. This is because the converted string includes a decimal point and can contain sign and exponent information. There is no provision for overflow. _gcvt attempts to produce digits digits in decimal format. If it cannot, it produces digits digits in exponential format. Trailing zeros may be suppressed in the conversion.

    Example

    /* _GCVT.C: This program converts -3.1415e5
    * to its string representation.
    */

    #include <stdlib.h>
    #include <stdio.h>

    void main( void )
    {
    char buffer[50];
    double source = -3.1415e5;
    _gcvt( source, 7, buffer );
    printf( "source: %f buffer: '%s'\n", source, buffer );
    _gcvt( source, 7, buffer );
    printf( "source: %e buffer: '%s'\n", source, buffer );
    }

    Output

    source: -314150.000000 buffer: '-314150.'
    source: -3.141500e+005 buffer: '-314150.'

    MSDN bietet hierfür was schönes... 😉 ...und dein char-Array enthält ja dann schließlich die einzelnen Ziffern, auf die du wahlfrei sequenziell zugreifen kannst.
    Geht natürlich auch mit "sscanf".



  • Wenn du eine Dezimalzahl in einem string haben willst, dann mach es so wie in folgendem FAQ-Beitrag beschrieben:

    Einmal Zahl nach String und zurück

    P.S. Du kannst in einem std::string einzeln auf die chars zugreifen, wie bei einem normalen Array.



  • s.u.



  • danke... hat mir weitergeholfen! 🙂



  • ich benutze linux hat jemand den code fer funktion _gcvt , schheint nicht in meiner header datei zu sein



  • Hallo,

    sollte aber in deiner Header-Datei stdlib.h sein, wenn auch ohne "underscore", denn:

    http://www.die.net/doc/linux/man/man3/gcvt.3.html

    MfG



  • natürlich solltest du <cstdlib> includieren, wie es sich für ein C++-Programm gehört 😉

    MfG



  • jetzt funktioniert alles... thx



  • kuehlwalda schrieb:

    string ClSTD_FLOAT_TO_STRING :: get_stelle(float x) 
    { 
    if(x>=0 && x<1) return "0";   
    else if(x>=1 && x<2) return "1"; 
    else if(x>=2 && x<3) return "2"; 
    else if(x>=3 && x<4) return "3"; 
    else if(x>=4 && x<5) return "4"; 
    else if(x>=5 && x<6) return "5"; 
    else if(x>=6 && x<7) return "6"; 
    else if(x>=7 && x<8) return "7"; 
    else if(x>=8 && x<9) return "8"; 
    else if(x>=9) return "9"; 
    }
    

    Das ist doch jetzt nicht dein Ernst, oder ? :xmas1:



  • ok, bitte vergessen.
    Ist persönliche Steinzeit und tut außerdem was anderes.

    Julio


Anmelden zum Antworten