开发需求中要求向上取5%, 比如结果是0.04 就要四舍五入为0.05
# Python写法
# 方法一:
def getNum(num):
limit_num = 0.05
if num % limit_num == 0:
print(num)
else:
num = math.ceil(num / limit_num) * limit_num
print(num)
# 方法二:
def getNums(num):
limit_num = 0.05
b = round(num, 1)
if b < num:
b = b + limit_num
print b
// C#写法
// 方法1:
static double getNum(double num)
{
double limit_num = 0.05; // 向上取N
if (num % limit_num == 0.05)
{
return num;
}
else
{
num = Math.Ceiling(num / limit_num) * limit_num;
return num;
}
}
// 方法2:
static double getNums(double num)
{
double limit_num = 0.05; // 向上取N
double b = Math.Round(num, 1);
if (b<num)
{
b += limit_num;
}
return b;
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)