In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 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.
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.