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)