• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            ?

            ???? 可能有好多人,包括C語言老手都不知道如何將float數據轉換為string,我就是這樣,今天查了一下MSDN,才知道C提供了_gcvt函數實現這個功能,收獲著實不小,為了方便自己查詢,也為了那些像我這樣的網友能夠了解該函數的具體用法,我把MSDN的原文原封不動抄錄如下:

            _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.'
            						


            Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=708935


            [收藏到我的網摘]?? skyman_2001發表于 2006年05月05日 11:28:00



            #??lanno?發表于2006-05-05 18:26:00??IP: 222.35.68.*
            長見識了,俺總是這樣轉化的:
            char str[260];
            float f = -3.1415926;
            float f1 = 263e-5;
            sprintf(str,"%f",f);
            printf("%s\n",str);
            sprintf(str,"%f",f1);
            printf("%s\n",str);

            #??Skyman?發表于2006-05-05 21:35:00??IP: 202.202.10.*
            你這樣做也行
            但沒有這樣靈活
            是吧?

            #??zhnde?發表于2006-05-11 20:15:00??IP: 129.69.212.*
            靈活嗎? 能具體解釋一下嗎?

            #??Skyman?發表于2006-05-11 22:03:00??IP: 202.202.10.*
            至少可以方便的控制有效數字的位數啊。

            #??ztwaker?發表于2006-07-28 12:40:00??IP: 61.144.207.*
            /*C++ impl*/
            string cvt(const float fval)
            {
            stringstream ss;
            ss << fval;
            return ss.str();
            }

            ......///

            #??ztwaker?發表于2006-07-28 12:46:00??IP: 61.144.207.*
            加上有效數字位數控制

            string cvt(const float f, const int prec)
            {
            stringstream ss;
            ss.precision(prec);
            ss << f;
            return ss.str();
            }

            posted on 2006-12-15 11:18 清源游民 閱讀(9231) 評論(1)  編輯 收藏 引用 所屬分類: C++

            FeedBack:
            # re: 如何將float轉換為string
            2011-09-06 09:56 | 明樣
            不知道_gcvt是C標準函數還是只是MS的擴展?
            如果不是標準函數的話還是不建議使用,沒有移值性就談不上靈活……  回復  更多評論
              
            <2025年5月>
            27282930123
            45678910
            11121314151617
            18192021222324
            25262728293031
            1234567

            留言簿(35)

            隨筆分類(78)

            隨筆檔案(74)

            文章檔案(5)

            搜索

            •  

            最新評論

            閱讀排行榜

            評論排行榜

            99久久精品免费看国产| 国产精品免费久久久久久久久| 国产精品久久久亚洲| 亚洲欧美一级久久精品| 国产高清国内精品福利99久久 | 99久久成人国产精品免费| 亚洲人成无码网站久久99热国产 | 久久精品国产99国产精品| 99久久国产免费福利| 欧美一区二区精品久久| 88久久精品无码一区二区毛片| 久久伊人精品青青草原高清| 久久免费视频观看| 99久久国产综合精品成人影院| 欧美精品九九99久久在观看| 国产午夜精品理论片久久影视| 狠色狠色狠狠色综合久久| 一本大道加勒比久久综合| 久久久久婷婷| 99精品国产免费久久久久久下载| 久久播电影网| 日韩精品国产自在久久现线拍| 伊人丁香狠狠色综合久久| 日韩久久久久中文字幕人妻| 波多野结衣AV无码久久一区| 久久久久久夜精品精品免费啦| 久久er热视频在这里精品| 久久久99精品成人片中文字幕| 欧美一区二区久久精品| 久久久久久久免费视频| 久久人人爽人人爽人人片AV不| 久久九九青青国产精品| 久久这里有精品视频| 99蜜桃臀久久久欧美精品网站| 91精品国产乱码久久久久久| 99久久国产精品免费一区二区| 99999久久久久久亚洲| 污污内射久久一区二区欧美日韩| 久久免费的精品国产V∧| 久久精品国产只有精品66| 亚洲精品无码久久一线|