In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the knowledge of "how to neatly format the floating-point data in the program in C++". Many people will encounter this dilemma in the operation of actual cases. then let the editor lead you to learn how to deal with these situations! I hope you can read it carefully and be able to achieve something!
The following are detailed instructions on how to "neatly" format the floating-point data in the program in C++. Here we have a circuitous way to convert them into strings, format them and display them in text form, which I believe is a headache for some technicians.
If there is a function in C++ that takes an long double parameter and converts it to a string, the resulting string should retain two decimal places. For example, a floating-point value of 123.45678 should generate a string like "123.45". On the face of it, this is not a meaningful programming problem, but if it is to be used in practice, the function should be designed to be flexible to allow the caller to specify decimal places. In addition, the function should be able to handle all kinds of exceptions, such as integers like 123.0 or 123s.
Before we begin, take a look at two "mantras" when writing the "elegant" C++ code:
Mantra 1: whenever you need to format a numeric value, it should be converted to a string first. This ensures that each number occupies exactly one character.
Mantra 2: use the library when you need to convert to a string.
The interface of the conversion function is very simple: * the parameters are the values to be formatted; the second parameter represents the decimal places displayed after the decimal point and should have a default value; the return value is a string type:
The following is the complete code of do_fraction (): string do_fraction (long double value, int decplaces=3) {ostringstream out; int prec= numeric_limits::digits10; / / 18 out.precision (prec); / / overrides the default precision out
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.