char - > INT [英] char -> int

查看:43
本文介绍了char - > INT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都知道从547.6458679开始的快速而肮脏的功能。

整数版本548?


ie


int returnIntegerEquivalent(char * charNum){

int intNum;

//做一些事情......

返回intNum;

}


我之前使用过Windows的东西(即_gcvt()和atof())。

谢谢,

解决方案



ern写道:

任何人都知道从547.6458679开始的快速而肮脏的功能
整数版548?


#include< stdlib.h>

#include< ; math.h>

int returnIntegerEquivalent(char * charNum){
int intNum;


double dNum = strtod(charNum,NULL);

if(ceil(dNum) - dNum> 0.5)

intNum =(int)floor(dNum);

else

intNum =(int)ceil(dNum);

return intNum;
}

之前我正在使用Windows的东西(即_gcvt()和atof())。
谢谢,




ern写道:

任何人都知道从547.6458679开始的快速而肮脏的功能。到整数版本548?

int returnIntegerEquivalent(char * charNum){
int intNum;
//做一些东西...


intNum = atof(charNum)+ 0.5;

返回intNum;
}

我之前正在使用Windows的东西(即_gcvt()和atof())。
谢谢,




" ern" < ER ******* @ gmail.com>在消息中写道

news:11 ********************* @ g14g2000cwa.googlegro ups.com ...

任何人都知道来自547.6458679的快速而肮脏的功能。要整数版本548?




四舍五入到无穷大是用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,

解决方案


ern wrote:

Anybody know a quick and dirty function for going from "547.6458679" to
the integer version 548 ?

i.e.

#include <stdlib.h>
#include <math.h>
int returnIntegerEquivalent(char * charNum){
int intNum;
double dNum = strtod(charNum, NULL);
if (ceil(dNum) - dNum > 0.5)
intNum = (int) floor(dNum);
else
intNum = (int) ceil(dNum);
return intNum;
}

I was using the Windows stuff before (ie _gcvt() and atof()).
Thanks,




ern wrote:

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...
intNum = atof(charNum) + 0.5;
return intNum;
}

I was using the Windows stuff before (ie _gcvt() and atof()).
Thanks,




"ern" <er*******@gmail.com> wrote in message
news:11*********************@g14g2000cwa.googlegro ups.com...

Anybody know a quick and dirty function for going from "547.6458679" to
the integer version 548 ?



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 - &gt; INT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆