浮点数四舍五入到小数点后两位

#include<stdio.h>
#include<math.h>
double change(double n){
    return round(n * 100) / 100;
}
int main(){
    double n;
    while(scanf("%lf",&n)!= EOF){
        printf("%.2f",change(n));
    }
    return 0;
}

即:将浮点数乘 100,求其四舍五入整数,然后除以100,便可得到 相对应的值

原文地址:https://www.cnblogs.com/expedition/p/12318349.html

时间: 2024-08-04 13:11:48

浮点数四舍五入到小数点后两位的相关文章

Double类型的数据四舍五入保留小数点后两位

4种方法,都是四舍五入,例: import java.math.BigDecimal; import java.text.DecimalFormat; import java.text.NumberFormat; public class format { double f = 111231.5585; public void m1() { BigDecimal bg = new BigDecimal(f); double f1 = bg.setScale(2, BigDecimal.ROUND

mysql格式化小数保留小数点后两位(小数点格式化)

格式化浮点数的问题,用format(col,2)保留两位小数点,出现一个问题,例如下面的语句,后面我们给出解决方法 SELECT FORMAT(12562.6655,2); 结果:12,562.67 查看文档:Formats the number X to a format like '#,###,###.##', rounded to D decimal places, and returns the result as a string. If D is 0, the result has

java 取小数点后两位 不四舍五入,怎么做

java 取小数点后两位 不四舍五入,怎么做 正常版: //正常版: import java.text.DecimalFormat; import java.math.RoundingMode; DecimalFormat formater = new DecimalFormat(); formater.setMaximumFractionDigits(2); formater.setGroupingSize(0); formater.setRoundingMode(RoundingMode.F

【java】java处理随机浮点数(小数点后两位)用RMB的大写数值规则输出

晚上上床前,拿到这个有意思的问题,就想玩弄一番: ============================================================================ 规则:[随机浮点数按照RMB读法写出] 总代码如下: package com.sxd.test; import org.junit.Test; import java.text.DecimalFormat; import java.util.Random; /** * @Author SXD *

Android 保留小数点后两位,并采取四舍五入

//小数点后两位四舍五入 private double formatDouble2(double d) { BigDecimal bigDecimal = new BigDecimal(d); double bg = bigDecimal.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue(); return bg; } 原文地址:https://www.cnblogs.com/niupi/p/11428915.html

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

Java中取小数点后两位(四种方法)

摘自http://irobot.iteye.com/blog/285537 Java中取小数点后两位(四种方法) 一 Long是长整型,怎么有小数,是double吧     java.text.DecimalFormat   df=new   java.text.DecimalFormat("#.##");     double   d=3.14159;     System.out.println(df.format(d)); 二 java.math.BigDecimal     B

JAVA除法保留小数点后两位的两种方法 Java Math的 floor,round和ceil的总结

floor 返回不大于的最大整数 round 则是4舍5入的计算,入的时候是到大于它的整数round方法,它表示“四舍五入”,算法为Math.floor(x+0.5),即将原来的数字加上0.5后再向下取整,所以,Math.round(11.5)的结果为12,Math.round(-11.5)的结果为-11. ceil 则是不小于他的最小整数 看例子   Math.floor Math.round Math.ceil 1.4 1 1 2 1.5 1 2 2 1.6 1 2 2 -1.4 -2 -1

js如何实现数字保留小数点后两位小数

js如何实现数字保留小数点后两位小数:小数点后保留两位小数是比较常见的形式,由于比较简单,下面直接给出例子.代码如下: var num=3.1415926; console.log(num.toFixed(2)); toFixed()函数可以参阅javascript的Number对象的toFixed()方法一章节. 原文地址是:http://www.softwhy.com/forum.php?mod=viewthread&tid=9567 更多内容可以参阅:http://www.softwhy.