double类型之四舍五入

题目:

A - Tutor

Time Limit:1000MS     Memory Limit:65535KB     64bit IO Format:%I64d & %I64u

Submit 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 cases. 
Every case will be twelve lines. Each line will contain the money she earned per month. Each number will be positive and displayed to the penny. No dollar sign will be included.

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

分析:整体来说题目理解相当容易,只要是将题目意思搞定就可以敲出一行得到结果的代码。但是在处理四舍五入的问题时总是没法好好的搞定。而且这也不是第一次遇见这类东西了。现在先上关键代码后分析。

 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 };

int zhangjie = (int)(sum * 1000);
        zhangjie+=5;
        zhangjie/=10;运算的结果是要求保留到小数点后两位,然后当然它就是和小数点的后三位是相关连的。我们先将结果*1000后取整,相当于将后面所有的部分全部的给去掉。然后的加五是为了实现四舍五入的效果。最后的除法也是为了去掉最后一位。在这里最关键的一点就是要十分的清楚,int和除法这两种操作都是直接的将最后面的部分给去掉。

然后就是在实现浮点数四舍五入的过程中,有函数ceil和函数floor可以实现的,

比如这个:

int round(double x){	return (x - floor(x) >= 0.5) ? (int)ceil(x) : (int)floor(x);}
时间: 2024-08-07 12:28:24

double类型之四舍五入的相关文章

mysql float double 类型

1.float类型 float列类型默认长度查不到结果.必须指定精度. 比方 num float, insert into table (num) values (0.12); select * from table where num=0.12的话.empty set. num float(9,7), insert into table (num) values (0.12); select * from table where num=0.12的话会查到这条记录. mysql> create

Java中double类型的数据精确到小数点后两位

Java中double类型的数据精确到小数点后两位 多余位四舍五入,四种方法 一: double f = 111231.5585;BigDecimal b = new BigDecimal(f); double f1 = b.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue(); 二: new java.text.DecimalFormat("#.00").format(3.1415926) 三: double d = 3.1415926

Double 类型运算时的精度问题

double 类型运算时的 计算的精度不高,经常会出现0.999999999999999这样的情况,那么就需要用BigDecimal   它是java提供的用来高精度计算的工具类 下面是对这个类的一个包装,方便使用: package cn.soft.util; import java.io.Serializable; import java.math.BigDecimal; import org.springframework.stereotype.Component; /** *类描述: do

java double类型计算出现的精度问题

 double d=100*1.005; 结果d的结果为100.49999999999999 怎么解决这种问题呢,用DecimalFormat,它对double类型的进行四舍五入处理用法如下: DecimalFormat df=new DecimalFormat(pattern): String nums=df.format(value): 这里给pattern:".00" value:100*1.005 nums值为:100.50

java中double类型显示两个小数,比如12.00

Double类型的数据如何保留两位小数? 各位大虾,现有Double类型的数据,如何转换为保留两位小数的数,返回值的类型仍然是Double类型的,而不是字符串类型. 比如     0,返回"0.00": 提示:DecimalFormat       df       =       new       DecimalFormat( "#####0.00 ");                       System.out.println(df.format(d)

Java中如何判断一个double类型的数据为0?

Java中如何判断一个double类型的数据为0 其实这个问题很简单,只是很多时候考虑复杂了,直接用==判断即可.下面给出测试例子: /**  * 如何判断一个double类型的数据为0  *  * @author leizhimin 2014/8/27 10:31  */ public class Test4 {     public static void main(String[] args) {         double x = 0.00000000000000000;       

OpenMesh 将默认的 float 类型改为 double 类型

OpenMesh 中默认的数据类型都是 float 类型的,如果要将其默认的 float 类型改为 double 类型,可以这么做: #include <OpenMesh/Core/Mesh/PolyMesh_ArrayKernelT.hh> #include <OpenMesh/Core/IO/MeshIO.hh> #include <OpenMesh/Core/Mesh/Handles.hh> #include <OpenMesh/Core/Mesh/Trai

double类型的数值在EditText中显示?

============问题描述============ double类型的数值在EditText中显示?et_count.setText(double型数值) 报错,请问应该怎么写? ============解决方案1============ 你要用String.valueof(double value) ============解决方案2============ et_count.setText(String.valueOf(double型数值)) ============解决方案3====

float及double类型减法运算时精度丢失问题

当float和double类型在进行减法运算时,会出现精度丢失问题,这种问题主要是由于计算机中普遍使用2进制所造成的.在此做为记录,防止日后遗忘.     public static void main(String[] args) {         float a = 2.1f;         float b = 2.0f;         float c = a - b;         System.out.println("a-b=" + c);     }     //输