Rounding of the Double Type

Source: Internet
Author: User

Question:

A-tutor Time limit:1000 ms Memory limit:65535kb 64bit Io format:% I64d & % i64usubmit status

Description

Lilin was a student of Tonghua Normal University. she is studying at University of Chicago now. besides studying, she worked as a tutor teaching Chinese to Americans. so, she can earn some money per month. at the end of the year, Lilin wants to know his average monthly money to decide whether continue or not. but she is not good at calculation, so she ask for your help. please write a program to help Lilin to calculate the average money her earned per month.

Input

The first line contain one integer T, means the total number of instances.
Every case will be twelve lines. Each line will be contain the money she earned per month. Each number will be positive and displayed to the penny. No dollar sign will be supported ded.

Output

The output will be a single number, the average of money she earned for the twelve months. it will be rounded to the nearest penny, preceded immediately by a dollar sign without tail zero. there will be no other spaces or characters in the output.

Sample Input

2 100.00 489.12 12454.12 1234.10 823.05 109.20 5.27 1542.25 839.18 83.99 1295.01 1.75 100.00 100.00 100.00 100.00 100.00 100.00 100.00 100.00 100.00 100.00 100.00 100.00

Sample output

$1581.42 $100 analysis: as a whole, the question is quite easy to understand. As long as you get the meaning of the question, you can click a line of code to get the result. But it is always hard to solve the Rounding Problem. And this is not the first time we have met such things. Now let's analyze the key code first.
 1 #include <iostream> 2 #include <cstdio> 3  4 using namespace std; 5  6 int main(){ 7     int t; 8     double a; 9     cin>>t;10     while(t--){11         double sum = 0;12         for(int i = 0;i < 12; i++){13             cin>>a;14             sum+=a;15         }16         sum/=12;17         int zhangjie = (int)(sum * 1000);18         zhangjie+=5;19         zhangjie/=10;20         sum=zhangjie/100.0;21         if((int)(sum*100)%10!=0)22         printf("$%.2lf\n",sum);23         else if((int)(sum*10)%10!=0)24         printf("$%.1lf\n",sum);25         else printf("$%.0lf\n",sum);26     }27 return 0;28 };
View code
Int Zhangjie = (INT) (sum * 1000); Zhangjie + = 5; Zhangjie/= 10;
The result of the operation is to retain the two digits after the decimal point, and of course it is related to the last three digits of the decimal point. First, we get the result * 1000 and then an integer, which is equivalent to removing all the following parts. And then add 5 to realize the rounding effect. The last division is to remove the last one. The most important thing here is to be very clear. The int and Division operations directly remove the last part.
Then, the function Ceil and function floor can be implemented in the Process of rounding floating point numbers. For example:
int round(double x)
{
return (x - floor(x) >= 0.5) ? (int)ceil(x) : (int)floor(x);
}

Rounding of the Double Type

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.