可能有好多人,包括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.'
分享到:
相关推荐
C++ VS2015 CString string int float各种类型转换,MFC对话框实现对各种类型数据转换并显示
在VS2017环境中编译通过,代码读取指定位置的.txt文档,使用文件流的形式读取,将文档里面全部是数字的string类型数据转换成对应的float类型的数据存储到指定的数组。
Convertor按照IEEE 754标准对Float和Double类型进行转换
各种基本数据类型转换源程序。 本程序涉及到的数据类型有:float, int , char, string.
一次作业,将浮点型转换成任意长度的字符串。适用于不仅限于浮点型
Python 如何转换string到float?简单几步,让你轻松解决。 打开软件,新建python项目,如图所示 右键菜单中创建.py文件,如图所示 步骤中文件输入代码如下: def string_to_float(str): return float(str) if __...
今天小编就为大家分享一篇Android String类型转换为float、double和int的工具类方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
一个将字符串转换为浮点型的函数,用于上位机与单片机的数据传输。该函数只需传入一个字符串数组(指针)与一个用于存放转换数据的double类型的数组,即可完成数据的转换。该函数对任意浮点数都有效(无论正负与位数...
自己初学python写的string转float,运行成功了,有需要的可以下载也多一种思路。
浮点数在内存中的存储机制和整型数...1、将字符串转换为float、double过程存在精度损失,只是float、double各自损失的精度不相同而已std::string str=”8.2″;float cc=atof(str.c_str()); //cc的值为8.1999998std:
如果可以将值转换为指定类型而不会丢失数据,则函数返回 true,如果不能,则返回 false。 还包括三个补充函数: to_int 、 to_float和to_string 。 如果相应的safe_函数返回 true,则这些函数转换并返回一个值,...
第二个参数为基数,可选2~36 注:对于无符号整形,可以使用FormatUint(i uint64, base int) (3)string转int i, err := strconv.Atoi(s) (4)string转int64 i, err := strconv.ParseInt(s, 10, 64) 第二个
1如何将字串 String 转换成整数 int? A. 有两个方法: 1). int i = Integer.parseInt([String]); 或 i = Integer.parseInt([String],[int radix]); 2). int i = Integer.valueOf(my_str).intValue(); 注: ...
一些初学JAVA的朋友可能会遇到JAVA的数据类型之间转换的苦恼,例如,整数和float,double型之间的转换,整数和String类型之间的转换,以及处理、显示时间方面的问下面笔者就开发中的一些体会介绍给大家。 我们知道,...
Trans(double)返回2进制 TransToHex(double)返回16进制 GetCrcstring(string data, ref string crc)返回冗余校验
用的时候,发现很少,就自己写了一个,用c#实现的十六进制转浮点数。
转换为float 转换为bool 转换为complex 转换为string 转换为bytes 转换为list 转换为tuple 转换为set 转换为dict 概述 数据类型转换,指的是通过某种方法,将一个数据由原来的类型转换为另外一个类型。比如,我们将...