zoukankan      html  css  js  c++  java
  • iOS 小数 变成 整数

    几种取整方法验证程序,如下,可以编译运行体会下
    int强制转换是不保留小数的。
    ceil是向上取整,floor是向上取整,这两个函数返回值是double类型的
    c语言中没有四舍五入函数,需要使用时可以自己定义下。
    #include <math.h>#include <stdio.h>// 自定义四舍五入宏#define ROUND(x) (int)(x + 0.4999999999999999)double round(double x, int d) // d为保留数字位数{ double y = pow(10, d); return floor(x * y + 0.5) / y;}double round(double x, double d) // d为圆整精度{ return floor(x / d + 0.5) * d;}void main(){ double a = 3.0; int i; for(i=0; i<10; i++, a+=0.1) printf("%f %d %d %d %d ", a, (int)a, (int)ceil(a), (int)floor(a), ROUND(a)); double x = 53.1415926; for(i=-1; i<5; i++) printf("%f ", round(x, i)); printf("%f ", round(x, 0.037));}

  • 相关阅读:
    (Problem 4)Largest palindrome product
    (Problem 3)Largest prime factor
    (Problem 2)Even Fibonacci numbers
    (Problem 1)Multiples of 3 and 5
    Ural 1086
    Ural 1319
    Ural 1149
    Ural 1079
    Ural 1068
    2016/04/06
  • 原文地址:https://www.cnblogs.com/yecong/p/6124689.html
Copyright © 2011-2022 走看看