将我的十进制浮点数舍入到最接近的一半或整数? [英] rounding my decimal float to nearest half or whole number?

查看:136
本文介绍了将我的十进制浮点数舍入到最接近的一半或整数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的我在我的应用程序中遇到了我的计算问题而且我很确定这是由于小数值很长。这是需要做的事情,因为它会大大缩短我需要的大量代码。

Ok im having issues with my calculations in my app and im pretty sure that it is due to the decimals being to long. This is something that needs to be done anyway because it will drastically shortin the ammount of code i need.

下面是我目前如何拥有这部分代码...

below is how i currently have this portion of my code...

float ab1 = [abdomen1Input.text floatValue];
float ab2 = [abdomen2Input.text floatValue];
float ab3 = [abdomen3Input.text floatValue];
float hip1 = [hips1Input.text floatValue];
float hip2 = [hips2Input.text floatValue];
float hip3 = [hips3Input.text floatValue];
float neck1 = [neck1Input.text floatValue];
float neck2 = [neck2Input.text floatValue];
float neck3 = [neck3Input.text floatValue];
float height1 = [heightInput.text floatValue];
float neckAvg;
float abHipAvg;
float cirVal;

(abHipAvg = (((ab1+ab2+ab3)/3)+((hip1+hip2+hip3)/3)));

(neckAvg = ((neck1+neck2+neck3)/3));

(cirVal = (abHipAvg - neckAvg));

我需要做的是将abHipAvg和neckAvg四舍五入到最近的.5或.0。用户只会输入.25,.5或.75但是一旦我将它们除以3,我需要将该答案向上或向下舍入到最接近的一半,以便我的其余计算正常工作。我已经尝试在这里查看一些问题以找到解决方案,因为我之前看到过一个与此类似的问题,因为我无法记住在搜索中输入的位置或内容。如果有人能指出我正确的方向,我会非常感激。

what i need to do is round the "abHipAvg" and "neckAvg" to the nearest .5 or .0 .Users are only going to be entering .25 , .5 or .75 but once i divide those by 3 i need to round that answer up or down to the nearest half for the rest of my calculations to work properly. i have tried looking through some questions on here to find a solution because i have seen one similar to this before i just cant remember where or what to type in the search. If anyone can point me in the right direction i would really appreciate it.

推荐答案

最简单的方法是拿走你的最终号码,时间2和圆。然后除以2.

The easiest way is to take your final number, times 2 and round. Then divide by 2.

(abHipAvg = (((ab1+ab2+ab3)/3)+((hip1+hip2+hip3)/3)));
abHipAvg = roundf(abHipAvg * 2.0f) / 2.0f;

与neckAvg相同。

and the same with neckAvg.

这篇关于将我的十进制浮点数舍入到最接近的一半或整数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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