In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-17 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.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.