如何将百分比四舍五入为整数

2024-07-01 07:11:43 发布

您现在位置:Python中文网/ 问答频道 /正文

示例:

150 blue balls
250 red balls

蓝球的百分比是多少?在

150除以总球数(400)等于38%

如果我设置它

^{pr2}$

我仍然是十进制的(浮动的),它不是四舍五入的。 我试着补充

int(round(float(blue_balls)))

运气不好

如何让它以整数形式给出答案?没有小数

谢谢

澄清-这是我写的

number_of_blue_balls = int(input('Enter number of blue balls:'))
number_of_red_balls = int(input('Enter number of red balls:'))

blue_balls= 1.0 * number_of_blue_balls / (number_of_blue_balls + number_of_red_balls) * 100
red_balls = 1.0 * number_of_red_balls / (number_of_blue_balls + number_of_red_balls) * 100

int(round(float(blue_balls)))
int(round(float(red_balls)))

print("Percent blue balls:", blue_balls,'%')
print("Percent red balls:", red_balls,'%')

我试着简化代码,得到了同样的答案,我只需要再多写一行,把浮点数转换成整数/百分比

1 number_of_blue_balls=int(输入('输入蓝色球的数量:')

2个红球数量=int(输入('输入红球数量:')

4个蓝球=int(蓝蓝球数/(蓝球数+红球数)*100

5个红球=int(红球数/(蓝球数+红球数)*100

7个印刷品(“蓝色小球百分比:”,蓝色小球)

8个印刷品(“红球百分比:”,红球)

终于拿到了!!!当我添加.5和couple sep=''时,我得到了我需要的结果

谢谢大家! 1 number_of_blue_balls=int(输入('输入蓝色球的数量:')

2个红球数量=int(输入('输入红球数量:')

4个蓝球=int(蓝球数/(蓝球数+红球数)*100+0.5)

5个红球=int(红球数/(蓝球数+红球数)*100)

7打印(“蓝色小球百分比:”,蓝色小球,“%”,sep='')

8打印(“红球百分比:”,红球,“%”,sep='')


Tags: ofnumber数量blueredfloatsepint
3条回答

试试这个

per = 1.0 * number_of_blue_balls / (number_of_blue_balls + number_of_red_balls) 
per = int(per * 100 + 0.5)

使用math.ceil怎么样

import math

print int(math.ceil(1.0*150 / (150+250) *100))

我明白了-答案是

1 number_of_blue_balls=int(输入('输入蓝色球的数量:')

2个红球数量=int(输入('输入红球数量:')

4个蓝球=int(蓝球数/(蓝球数+红球数)*100+0.5)

5个红球=int(红球数/(蓝球数+红球数)*100)

7打印(“蓝色小球百分比:”,蓝色小球,“%”,sep='')

8打印(“红球百分比:”,红球,“%”,sep='')

相关问题 更多 >

    热门问题