Rounding of nearest 0.5

2020-03-26 08:09发布

I want to be rounded off this way

13.1, round to 13.5
13.2, round to 13.5
13.3, round to 13.5
13.4, round to 13.5
13.5 = 13.5
13.6, round to 14.0
13.7, round to 14.0
13.8, round to 14.0
13.9, round to 14.0

sorry for modification i need in the above way... did this way but not appropriate

doubleValue = Math.Round((doubleValue * 2), MidpointRounding.ToEven) / 2;

标签: c# rounding
7条回答
看我几分像从前
2楼-- · 2020-03-26 08:50

A simple way of doing this without the builtin method of c# (if you want ) its writen i c++ (I once lacked the round function in c++ ) but you can easly change it to c# syntax

int round(float nNumToRound)
{

// Variable definition
int nResult;

// Check if the number is negetive
if (nNumToRound > 0)
{
    // its positive, use floor.
    nResult = floor(nNumToRound + 0.5);
}
else if (nNumToRound < 0)
{
    // its negtive, use ceil 
    nResult = ceil(nNumToRound - 0.5);
}

return (nResult);

}

查看更多
登录 后发表回答