Get the App
SLTechnology News&Howtos  ›  Development  › 

How to convert floating-point numbers into strings in C language

Shulou Source: shulou.com Published: 2022-06-02 06:58:44 09月17日 Update

This article shows you how the C language converts floating-point numbers into strings. The content is concise and easy to understand, which can definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

Convert floating point numbers to string 1, format float/double to string output

The standard C language provides atof functions to convert strings to double, but does not provide library functions to convert float/double to strings, but uses sprintf and snprintf functions to format the output to strings.

Function declaration:

Int sprintf (char * str, const char * format,...); int snprintf (char * str, size_t size, const char * format,...)

Example (book99.c)

Int sprintf (char * str, const char * format,...); int snprintf (char * str, size_t size, const char * format,...); example (book99.c) / * * program name: book99.c, this program demonstrates formatted output sprintf and snprintf functions. * author: C language Technology Network (www.freecplus.net) date: 20190525*/#include # include int main () {float ff=1024.58; double dd=12345678901234.58; char strff [21], strdd [21]; memset (strff,0,sizeof (strff)); memset (strdd,0,sizeof (strdd)); / / convert floating point ff to string and store it in strff. Sprintf (strff, "% 2f", ff); printf ("strff=%s\ n", strff); / / output strff=1024.58 / / convert the double-precision floating-point number dd to a string and store it in strdd. Sprintf (strdd, "% 2lf", dd); printf ("strdd=%s\ n", strdd); / / output strdd=12345678901234.58 memset (strff,0,sizeof (strff)); memset (strdd,0,sizeof (strdd)); / / convert the floating point ff to a string and store it in strff, keeping only the first 10 characters. Snprintf (strff,11, "% 2f", ff); printf ("strff=%s\ n", strff); / / output strff=1024.58 / / convert a double-precision floating-point number dd to a string, store it in strdd, and retain only the first 10 characters. Snprintf (strdd,11, "% 2lf", dd); printf ("strdd=%s\ n", strdd); / / output strdd=1234567890}

Running result

2. Points for attention

The performance of the snprintf function is slightly different on unix and windows platforms. On Linux platforms, size-1 characters are retained, and on windows platforms, size characters are retained.

Conversion between integers, floats, and strings 1. Integer, floating-point-- > string

Convert integers to strings:

Convert a floating-point value to a string:

two。 String-- > integer, floating point

The following functions convert strings to numbers:

The above is how the C language converts floating-point numbers into strings. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Tags: Characters strings points functions output languages floats platforms formats content skills knowledge examples programs precision differences conciseness conciseness matters authors Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB Shulou Tech Info OPPO Reno Microsoft Docker