如何将double转换为short? [英] how to convert double to short ?
问题描述
如何将double转换为short?
例如,我想转换
double doubleVal1 = 15000.1;
double doubleVal2 = 12000.0;
short shortVal;
shortVal = doubleVal1 - doubleVal2;
我想要结果shortVal = 3000并存储为短而不是双倍
值3000.1
我该怎么做?
how to convert double to short ?
for example, I want to convert
double doubleVal1 = 15000.1;
double doubleVal2 = 12000.0;
short shortVal;
shortVal = doubleVal1 - doubleVal2;
I want the result of shortVal = 3000 and store as short instead of double
value 3000.1
how can I do that?
推荐答案
[后续设置为acllcc ++]
Alan写道:
[Followups set to acllcc++]
Alan wrote:
如何将double转换为short?
例如,我要转换
double doubleVal1 = 15000.1;
double doubleVal2 = 12000.0;
short shortVal;
shortVal = doubleVal1 - doubleVal2;
我想要shortVal = 3000的结果并存储为short而不是
double value 3000.1
我该怎么做?
how to convert double to short ?
for example, I want to convert
double doubleVal1 = 15000.1;
double doubleVal2 = 12000.0;
short shortVal;
shortVal = doubleVal1 - doubleVal2;
I want the result of shortVal = 3000 and store as short instead of
double value 3000.1
how can I do that?
>
double doubleVal1 = 15000.1;
double doubleVal2 = 12000.0;
short shortVal;
shortVal = doubleVal1 - doubleVal2;
这不是轻浮答案。
这是一种更强大的方法来做同样的事情:
#include< limits.h>
#include< stdio.h>
int main(无效)
{
double doubleVal1 = 15000.1 ;
double doubleVal2 = 12000.0;
short shortVal;
if(doubleVal1 - doubleVal2< USHRT_MAX)
{
shortVal = doubleVal1 - doubleVal2;
}
else
{
printf(不能分配给shortVal - 结果太大了。\ n);
}
返回0;
}
-
Richard Heathfield: bi **** @ eton.powernet.co.uk
Usenet是一个奇怪的地方。 - Dennis M Ritchie,1999年7月29日。
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K& R答案,C书等:< a rel =nofollowhref =http://users.powernet.co.uk/etontarget =_ blank> http://users.powernet.co.uk/eton
double doubleVal1 = 15000.1;
double doubleVal2 = 12000.0;
short shortVal;
shortVal = doubleVal1 - doubleVal2;
This is not a flippant answer.
Here is a more robust way to do the same thing:
#include <limits.h>
#include <stdio.h>
int main(void)
{
double doubleVal1 = 15000.1;
double doubleVal2 = 12000.0;
short shortVal;
if(doubleVal1 - doubleVal2 < USHRT_MAX)
{
shortVal = doubleVal1 - doubleVal2;
}
else
{
printf("Can''t assign to shortVal - result is too large.\n");
}
return 0;
}
--
Richard Heathfield : bi****@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
2003年10月18日星期六15:46:54 +0800
" Alan" <人************* @ hotmail.com>写道:
On Sat, 18 Oct 2003 15:46:54 +0800
"Alan" <al*************@hotmail.com> wrote:
如何将double转换为short?
例如,我要转换
double doubleVal1 = 15000.1;
double doubleVal2 = 12000.0;
short shortVal;
shortVal = doubleVal1 - doubleVal2;
我想得到shortVal = 3000的结果并存储为short而不是
双倍价值3000.1
我该怎么做?
how to convert double to short ?
for example, I want to convert
double doubleVal1 = 15000.1;
double doubleVal2 = 12000.0;
short shortVal;
shortVal = doubleVal1 - doubleVal2;
I want the result of shortVal = 3000 and store as short instead of
double value 3000.1
how can I do that?
如果你不关心四舍五入的方向你刚刚完成它。
如果你关心圆角查找地板/ ceil的方向,并且如果使用C99圆形
。
-
马克戈登
支付成为极客&一位高级软件开发人员
虽然我的电子邮件地址是垃圾邮件,但它是真的,我读了它。
If you don''t care about the direction of rounding you''ve just done it.
If you do care about the direction of rounding look up floor/ceil and if
using C99 round.
--
Mark Gordon
Paid to be a Geek & a Senior Software Developer
Although my email address says spamtrap, it is real and I read it.
Mark Gordon写道:
Mark Gordon wrote:
如何将double转换为short?
[snip]
how to convert double to short ?
[snip]
如果你不关心舍入的方向你刚刚完成它。
If you don''t care about the direction of rounding you''ve just done it.
对于C99至少隐式转换的舍入方向和
从浮点类型转换为整数类型定义为朝向零的
,即截断(6.3.1.4第1节)。我没有C89
标准,但我怀疑那里也是如此。
祝你好运,
>
Sidney Cadot
For C99 at least the rounding direction of implicit conversions and
casts from floating-point types to integer types is defined as
towards-zero, i.e. truncating (6.3.1.4 clause 1). I don''t have a C89
standard handy, but I suspect the same is true there.
Best regards,
Sidney Cadot
这篇关于如何将double转换为short?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!