c++取整与精度问题_C/C++实战030:double转int常见的取整操作

在实际开发中我们经常会需要对数据类型进行转换,比如说要将double类型转成int类型。取整我们也需要根据自己的业务需求来得到自己想要的值,有的需要向上取整、有的可能需要向下取整,或者四舍五入取整。还有的直接只取整数部分,不管小数后面的内容。

666ad95f6f9aad3d695a03596a25b20f.png

向上取整

向上取整我们可以利用函数ceil来实现,该函数返回一个大于或等于x的整数。该函数由头文件math.h提供,比如π通过ceil返回的就是4。该函数可以处理float、double、long doubles数据类型及泛型,如果没有小数则返回直接参数X,如果有小数部分则舍去小数部分并向前进一位。当参数是string字符串时,则返回0。

ceil (double x);ceil (float x);ceil (long double x);ceil (T x);     // additional overloads for integral type---------------------------double π = 3.1415926;int a = ceil(π);print("%d",a);---------------------------4

向下取整

向下取整我们可以利用函数floor来实现,该函数返回一个不大于x的最大整数。该函数可以处理float、double、long doubles数据类型及泛型,如果没有小数则返回直接参数X,如果有小数部分则舍去小数部分并向后退一位。当参数是string字符串时,则返回0。

floor (double x);floor (float x);floor (long double x);floor (T x);     // additional overloads for integral type

四舍五入取整

四舍五入取整我们可以利用函数round来实现,返回距离参数最近的整数。该函数可以处理float、double、long doubles数据类型及泛型,如果参数值正好在两个整数的中间位置则返回距离0较远的那一个整数。

round (double x);round (float x);round (long double x);round (T x);           // additional overloads for integral types

只取整数部分

还有一种就是只取整数部分而不会四舍五入,不管小数后面的值是多少都直接舍弃。 如果你只想取整数部分那么只要在要转换的变量前用括号确定要转换的类型(int),这是种强制类型转换,是不安全的赋值方法。

(int) (double x);(int) (float x);(int) (long double x);(int) (T x);           // additional overloads for integral types

总结:

不同的数据有不同的数据类型,不同的数据类型具有不同的特性。不过不同类型的数据之间,有时候是可以相互转换的。数据类型转换就是因业务需求将数据从一种类型转换为另一种类型以满足业务需求,但是数据从“double”转换“int”会丢失数据精度所以慎用哦!以上内容是小编给大家分享的【C/C++实战030:double转int常见的取整操作】。希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。

  • 3
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
In file included from /home/yhdr/2-test-2023-06_v3/sent.h:24:0, from /home/yhdr/2-test-2023-06_v3/sent.cpp:1: /usr/include/c++/7/thread: In instantiation of ‘struct std::thread::_Invoker<std::tuple<void (*)(double*, double&, double&, double&, double&, double&), double**, std::reference_wrapper<double>, std::reference_wrapper<double>, std::reference_wrapper<double>, std::reference_wrapper<double>, std::reference_wrapper<double> > >’: /usr/include/c++/7/thread:127:22: required from ‘std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = void (&)(double*, double&, double&, double&, double&, double&); _Args = {double**, std::reference_wrapper<double>, std::reference_wrapper<double>, std::reference_wrapper<double>, std::reference_wrapper<double>, std::reference_wrapper<double>}]’ /home/yhdr/2-test-2023-06_v3/sent.cpp:18:153: required from here /usr/include/c++/7/thread:240:2: error: no matching function for call to ‘std::thread::_Invoker<std::tuple<void (*)(double*, double&, double&, double&, double&, double&), double**, std::reference_wrapper<double>, std::reference_wrapper<double>, std::reference_wrapper<double>, std::reference_wrapper<double>, std::reference_wrapper<double> > >::_M_invoke(std::thread::_Invoker<std::tuple<void (*)(double*, double&, double&, double&, double&, double&), double**, std::reference_wrapper<double>, std::reference_wrapper<double>, std::reference_wrapper<double>, std::reference_wrapper<double>, std::reference_wrapper<double> > >::_Indices)’ operator()() ^~~~~~~~ /usr/include/c++/7/thread:231:4: note: candidate: template<long unsigned int ..._Ind> decltype (std::__invoke((_S_declval<_Ind>)()...)) std::thread::_Invoker<_Tuple>::_M_invoke(std::_Index_tuple<_Ind ...>) [with long unsigned int ..._Ind = {_Ind ...}; _Tuple = std::tuple<void (*)(double*, double&, double&, double&, double&, double&), double**, std::reference_wrapper<double>, std::reference_wrapper<double>, std::reference_wrapper<double>, std::reference_wrapper<double>, std::reference_wrapper<double> >] _M_invoke(_Index_tuple<_Ind...>)
最新发布
06-07

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值