问题描述
任何人都知道从547.6458679开始的快速而肮脏的功能。
整数版本548?
ie
int returnIntegerEquivalent(char * charNum){
int intNum;
//做一些事情......
返回intNum;
}
我之前使用过Windows的东西(即_gcvt()和atof())。
谢谢,
四舍五入到无穷大是用ceil完成的()
例子:
547.6 - > 548
547.4 - > 548
547 - > 547
-547.4 - > -547
-547.6 - > -547
四舍五入到无穷大是用地板完成的()
例子:
547.6 - > 547
547.4 - > 547
547 - > 547
-547.4 - > -548
-547.6 - > -548
截断小数部分是通过转换/转换为整数来完成
类型:
547.6 - > 547
547.4 - > 547
547 - > 547
-547.4 - > -547
-547.6 - > -547
舍入到最接近的整数可以通过截断来实现,如果0.5是
首先[添加到] / [减去]被舍入的数字:
547.6 + 0.5 - > 548
547.4 + 0.5 - > 547
547 + 0.5 - > 547
-547.4-0.5 - > -547
-547.6-0.5 - > -548
类似的东西,
Alex
Anybody know a quick and dirty function for going from "547.6458679" to
the integer version 548 ?
i.e.
int returnIntegerEquivalent(char * charNum){
int intNum;
//Do some stuff...
return intNum;
}
I was using the Windows stuff before (ie _gcvt() and atof()).
Thanks,
rounding to +infinity is done with ceil()
examples:
547.6 -> 548
547.4 -> 548
547 -> 547
-547.4 -> -547
-547.6 -> -547
rounding to -infinity is done with floor()
examples:
547.6 -> 547
547.4 -> 547
547 -> 547
-547.4 -> -548
-547.6 -> -548
truncating the fractional part is done by converting/casting to integer
type:
547.6 -> 547
547.4 -> 547
547 -> 547
-547.4 -> -547
-547.6 -> -547
rounding to the nearest integer can be achieved from truncating if 0.5 is
first [added to]/[subtracted from] the number being rounded:
547.6+0.5 -> 548
547.4+0.5 -> 547
547+0.5 -> 547
-547.4-0.5 -> -547
-547.6-0.5 -> -548
Something like that,
Alex
这篇关于char - > INT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!