• <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數(shù)據(jù)轉(zhuǎn)換為string,我就是這樣,今天查了一下MSDN,才知道C提供了_gcvt函數(shù)實(shí)現(xiàn)這個(gè)功能,收獲著實(shí)不小,為了方便自己查詢,也為了那些像我這樣的網(wǎng)友能夠了解該函數(shù)的具體用法,我把MSDN的原文原封不動(dòng)抄錄如下:

            _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


            [收藏到我的網(wǎng)摘]?? skyman_2001發(fā)表于 2006年05月05日 11:28:00



            #??lanno?發(fā)表于2006-05-05 18:26:00??IP: 222.35.68.*
            長見識(shí)了,俺總是這樣轉(zhuǎn)化的:
            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?發(fā)表于2006-05-05 21:35:00??IP: 202.202.10.*
            你這樣做也行
            但沒有這樣靈活
            是吧?

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

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

            #??ztwaker?發(fā)表于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?發(fā)表于2006-07-28 12:46:00??IP: 61.144.207.*
            加上有效數(shù)字位數(shù)控制

            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 清源游民 閱讀(9241) 評(píng)論(1)  編輯 收藏 引用 所屬分類: C++

            FeedBack:
            # re: 如何將float轉(zhuǎn)換為string
            2011-09-06 09:56 | 明樣
            不知道_gcvt是C標(biāo)準(zhǔn)函數(shù)還是只是MS的擴(kuò)展?
            如果不是標(biāo)準(zhǔn)函數(shù)的話還是不建議使用,沒有移值性就談不上靈活……  回復(fù)  更多評(píng)論
              
            <2007年3月>
            25262728123
            45678910
            11121314151617
            18192021222324
            25262728293031
            1234567

            留言簿(35)

            隨筆分類(78)

            隨筆檔案(74)

            文章檔案(5)

            搜索

            •  

            最新評(píng)論

            閱讀排行榜

            評(píng)論排行榜

            国产精品9999久久久久| 久久久久国色AV免费观看| 午夜天堂av天堂久久久| 午夜精品久久久久久中宇| 久久精品亚洲精品国产色婷| 狠色狠色狠狠色综合久久| 久久精品无码一区二区三区日韩| 精品无码久久久久久久久久| 久久伊人五月丁香狠狠色| 狠狠久久亚洲欧美专区| 久久只这里是精品66| 国产一级持黄大片99久久| 久久免费视频1| 久久se精品一区二区| 中文字幕久久久久人妻| 国产成人无码精品久久久久免费| 99久久99久久精品国产片果冻| 久久综合久久久| 色欲综合久久中文字幕网| 污污内射久久一区二区欧美日韩 | 精品99久久aaa一级毛片| 亚洲欧美日韩中文久久| 久久久久国产一级毛片高清板| 69国产成人综合久久精品| 精品综合久久久久久98| 久久av高潮av无码av喷吹| 久久久青草青青亚洲国产免观| 中文精品久久久久人妻不卡| 热久久视久久精品18| 久久精品无码一区二区三区日韩| 久久久久四虎国产精品| 国内精品久久久久影院日本| 无码久久精品国产亚洲Av影片 | 久久香蕉国产线看观看99| 亚洲中文精品久久久久久不卡| 亚洲一区精品伊人久久伊人| 久久99国产一区二区三区| 久久激情五月丁香伊人| 久久久精品波多野结衣| 久久国产成人亚洲精品影院| 狠狠色综合久久久久尤物|