我的代码在这里
if (dUIAnswer == "yes" || dUIAnswer == "ya")
{
quote += (25 / 100) * quote;
}
if (coverageType == "full coverage")
quote += (50 / 100) * quote; ;
return quote;如果用户有酒后驾驶,我基本上会尝试将“报价”的值增加25%。请给我一个简单的解决方案的代码是可能的。
发布于 2018-11-19 20:01:23
问题是:在C#和类似的语言(C,C++,...)中,25 / 100或50 / 100的结果是0。
为什么?
在C#中,除以整数(int、long等)时通过整数,小数部分被截断- 0.25变为0。
因此,有一些解决方案:
1)只需在代码中编写0.25 (或0.5):
if (dUIAnswer == "yes" || dUIAnswer == "ya")
{
quote += 0.25 * quote;
}
if (coverageType == "full coverage")
quote += 0.5 * quote; ;
return quote;2)将其中一个numebrs转换为double或float,方法是附加后缀D (对于double)或F (对于float),或者在末尾添加一个.0 (这样做是一个double),或者使用(<type>)转换它
if (dUIAnswer == "yes" || dUIAnswer == "ya")
{
quote += (25.0 / 100D) * quote;
}
if (coverageType == "full coverage")
quote += ((float)50 / 100) * quote; ;
return quote;(您也可以显式地或通过附加M作为后缀将其转换为decimal )。
https://stackoverflow.com/questions/53374123
复制相似问题