C++ basic - numeric type conversation数值类型转换
时间:2014-06-21 10:09:58
收藏:0
阅读:240
What?
在计算中涉及不同数值类型的computation,这时候,C++会按照以下顺序进行转换
order:long double>double>float>unsigned long>long>unsigned int>int
eg. (1) 1/2=0(当1和2都是int时),结果也肯定是int;
(2)1.0/2=0.5(当1.0为double,2为int时),2会被转化成doubley因为double>int
syntax for type convertion:
static_cast<type>(value)
eg. (1)cout<<static_cast<double>(1)/2<<endl
result: 0.5
(2)cout<<static_cast<int>(tax*100)/100.0
effect:keep two digits after the decimal point(保留小数点后两位)
评论(0)