gcvt

gcvt

将浮点型数转换为字符串,取四舍五入

相关函数

ecvt,fcvt,sprintf

表头文件

#include <stdlib.h>

定义函数

char *gcvt(double number,size_t ndigits,char *buf);

函数说明

gcvt()用来将参数number转换成ASCII码字符串,参数ndigits表示显示的位数。gcvt()与ecvt()和fcvt()不同的地方在于,gcvt()所转换后的字符串包含小数点或正负符号。若转换成功,转换后的字符串会放在参数buf指针所指的空间。

返回值

返回一字符串指针,此地址即为buf指针。

范例

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

int main()
{
        double a = 123.45;
        double b = -1234.56;
        char *ptr;
        char *ret;
        int decpt,sign;
        ret = gcvt(a,5,ptr);
        printf("a value = %s\n",ret);
        printf("a value = %s\n",ptr);
        ret = gcvt(b,6,ptr);
        printf("b value = %s\n",ret);
        printf("b value = %s\n",ptr);

        return 0;
}

执行

a value = 123.45
a value = 123.45
b value = -1234.56
b value = -1234.56

man 

GCVT(3)                                   Linux Programmer's Manual                                  GCVT(3)

NAME
       gcvt - convert a floating-point number to a string

SYNOPSIS
       #include <stdlib.h>

       char *gcvt(double number, int ndigit, char *buf);

   Feature Test Macro Requirements for glibc (see feature_test_macros(7)):

       gcvt():
           Since glibc 2.12:
               _SVID_SOURCE ||
                   (_XOPEN_SOURCE >= 500 ||
                       _XOPEN_SOURCE && _XOPEN_SOURCE_EXTENDED) &&
                   !(_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600)
           Before glibc 2.12:
               _SVID_SOURCE || _XOPEN_SOURCE >= 500 || _XOPEN_SOURCE && _XOPEN_SOURCE_EXTENDED

DESCRIPTION
       The  gcvt()  function converts number to a minimal length null-terminated ASCII string and stores the
       result in buf.  It produces ndigit significant digits in either printf(3) F format or E format.

RETURN VALUE
       The gcvt() function returns the address of the string pointed to by buf.

ATTRIBUTES
       For an explanation of the terms used in this section, see attributes(7).

       ┌──────────┬───────────────┬─────────┐
       │Interface │ Attribute     │ Value   │
       ├──────────┼───────────────┼─────────┤
       │gcvt()    │ Thread safety │ MT-Safe │
       └──────────┴───────────────┴─────────┘

CONFORMING TO
       Marked as LEGACY in POSIX.1-2001.  POSIX.1-2008 removes the specification of gcvt(), recommending the
       use of sprintf(3) instead (though snprintf(3) may be preferable).

SEE ALSO
       ecvt(3), fcvt(3), sprintf(3)

COLOPHON
       This  page  is  part  of  release 4.04 of the Linux man-pages project.  A description of the project,
       information  about  reporting  bugs,  and  the  latest  version  of  this  page,  can  be  found   at
       http://www.kernel.org/doc/man-pages/.

                                                 2015-03-29                                          GCVT(3)

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值