How to convert float to string

Source: Internet
Author: User

There may be a lot of people, including C-language veterans who don't know how to convert float data to string, that's what I did, I checked MSDN today to know that C provides the _GCVT function to achieve this function, the harvest is really not small, in order to facilitate their own inquiries, And for those like me who can understand the exact usage of the function, I copy the MSDN text as follows:

_GCVT

Converts a floating-point value to a string and which it stores in a buffer.

Char *_gcvt ( double value, int digits, char *Buffer );

Routine Required Header Compatibility
_gcvt <stdlib.h> Win, Win NT

For additional compatibility information, 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 is 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 PO Ssible 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 is 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. '

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.