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 double/float into string in C language

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

Share

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

This article mainly explains "how to convert double/float into string in C language". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "how to convert double/float into string in C language".

Convert double/float to a string (with custom precision) char * double_to_string (double d, int decimal) {decimal = decimal < 0? 0: decimal; char * p; char dd [20]; switch (decimal) {case 0: sprintf (dd, ".0lf", d); break Case 1: sprintf (dd, "% .1lf", d); break; case 2: sprintf (dd, "% .2lf", d); break; case 3: sprintf (dd, "% 3lf", d); break Case 4: sprintf (dd, "% .4lf", d); break; case 5: sprintf (dd, "% .5lf", d); break; default: sprintf (dd, ".6lf", d); break } p = malloc (strlen (dd)); strcpy (pgradd); return p;} the header file has # include # include # include simple usage double d = 3.1415926 dstr [20]; sprintf (dstr, "% .2lf", d); / / reserve d to assign 2 decimal places to dstrfloat and double precision problems double and float

It has high double precision, 16 significant digits and 7 float precision. But double consumes twice as much memory as float, and the operation speed of double is much slower than that of float. The mathematical function names double and float in C language are different. Don't make mistakes, don't use double precision when you can use single precision (to save memory and speed up the operation).

Accuracy of float and double

Is determined by the number of Mantissa digits. Floating point number is stored in memory according to scientific counting, and its integer part is always an implied "1". Because it is constant, it can not affect the accuracy.

Float: 2 ^ 23 = 8388608, a total of seven digits, which means that there can be up to seven significant digits, but the absolute guarantee is six digits, that is, the precision of float is 6 to 7 significant digits.

Double: 2 ^ 52 = 4503599627370496, a total of 16 bits. Similarly, the precision of double is 15 bits.

At this point, I believe you have a deeper understanding of "how to convert double/float into strings in C language". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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