Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report