【问题标题】:how to get percentage rounded to an integer如何将百分比四舍五入为整数
【发布时间】:2015-12-05 23:43:01
【问题描述】:

例子:

150 blue balls
250 red balls

蓝球的百分比是多少?

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

如果我设置它

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

我仍然得到十进制(浮动)并且它没有四舍五入。 我尝试添加

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(input('输入蓝色球的数量:'))

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

3

4 blue_balls= int(number_of_blue_balls/ (number_of_blue_balls + number_of_red_balls) * 100

5 red_balls= int(number_of_red_balls/ (number_of_blue_balls + number_of_red_balls) * 100

6

7 print("蓝球百分比:", blue_balls)

8 print("红球百分比:", red_balls)

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

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

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

3

4 blue_balls= int(number_of_blue_balls/ (number_of_blue_balls + number_of_red_balls) * 100 + 0.5)

5 red_balls= int(number_of_red_balls/ (number_of_blue_balls + number_of_red_balls) * 100)

6

7 print("蓝球百分比:", blue_balls, "%", sep='')

8 print("红球百分比:", red_balls, "%", sep='')

【问题讨论】:

  • 那还是不行。我的答案还在浮动形式这里给你看我具体写了什么
  • 确切的输出是什么?
  • 我得到的输出如 100.0 % 0.0% 和 37.5 % 62.5 % 有两个问题,小数以及数字和百分号之间的空格

标签: python floating-point int percentage


【解决方案1】:

试试这个

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

【讨论】:

  • 这行得通,但是它得到了这样的结果——蓝色球 - 37.5,红色球有 62.5,所以我最终得到 38 和 63,总计 101%。现在我必须弄清楚如何使两个数字都等于 100
  • @PaulBernella 如果你只想得到整数,总和并不总是等于 100。例如,150 和 250,那么你也会得到 38 和 63
  • @PaulBernella 如果总数必须为 100%,请使用算术使其如此。 per_blue = int(per * 100 + 0.5)per_red = 100 - per_blue
  • 这不太行。我同意,它是代数的,希望我能想到这一点。我确信第二部分是正确的(从 100 中减去蓝色得到红色),但我仍在努力让第一个方程得到舍入和整数(整数)的百分比
  • @PaulBernella 查看您的原始代码,您没有将 int 的结果分配给任何东西 - 也许您还在犯那个错误?
【解决方案2】:

math.ceil怎么样

import math

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

【讨论】:

    【解决方案3】:

    我知道了-答案是

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

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

    3

    4 blue_balls= int(number_of_blue_balls/ (number_of_blue_balls + number_of_red_balls) * 100 + 0.5)

    5 red_balls= int(number_of_red_balls/ (number_of_blue_balls + number_of_red_balls) * 100)

    6

    7 print("蓝球百分比:", blue_balls, "%", sep='')

    8 print("红球百分比:", red_balls, "%", sep='')

    【讨论】:

    • 对这两个值使用不同的四舍五入可能对这组输入有效,但对其他输入无效。您有两个相互矛盾的要求,即对百分比进行四舍五入,并坚持要求它们相加为 100 - 必须放宽其中之一。
    【解决方案4】:

    要得到四舍五入的整数百分比形式的答案:

    给定blue_balls, red_balls

    denominator = blue_balls + red_balls
    numerator = blue_balls
    percent = (100 * numerator + denominator/2)/denominator
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多