In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the relevant knowledge of "how to achieve digital rounding on C++". In the operation of actual cases, many people will encounter such a dilemma. 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!
Problem description
Write a program that accepts a positive floating-point value and outputs an approximate integer value of that value. If the value after the decimal point is greater than or equal to 5, round up; less than 5, round down.
Enter description
Enter a positive floating point value
Output description
Outputs the approximate integer value of this value
Example
Input
5.5
Output
six
Analysis
This question is taken as a whole, we should first know the principle of int type casting in C++, and then it will be more advantageous to help us to achieve this problem. The cast of the Int type retains only the integer part and discards all the decimal parts. According to this principle, we can easily take out the decimal part of the input floating point number and compare it with 0. 5 to determine whether to enter 1. 5.
In fact, this problem has a more convenient way, because int forced conversion only retains the integer part, while the decimal part of the upward 1 is greater than 0.5, if the decimal is called 0.5, then it meets the requirements of more than 0.5 upward 1, less than 0.5 do not carry, and then you can write a more simple code.
Code 1
# include
Using namespace std
Int main ()
{
Double number
Cin > > number
Int int_number
Int_number = number/1
Doublelast_number = number-int_number
If (last_number > = 0.5)
Int_number=int_number+1
Int output = int (int_number)
Cout
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.