如何使用模表达式/处理大型整数

2024-04-25 07:00:05 发布

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

我想制作一个程序来计算x年后的人口数量。在

2002年人口总数为62亿,每年增长1.3%。在

我要用的公式是

population = ((1.013)**x) * 6.2B

如何使6.2B更易于使用?在


Tags: 程序公式population人口总数人口数量
1条回答
网友
1楼 · 发布于 2024-04-25 07:00:05

这是你的密码。读书学好。这可能是一个你可以用谷歌解决的问题。在

import math

def calculate_population(years_since_2002): #the original calculation
    population_2002 = 6.2*10**9
    final_population = int(((1.013)**years_since_2002)*population_2002)
    return final_population

def pretty_print(num,trunc=0):
    multiplier = int(math.log10(num)) #finds the power of 10
    remainder = float(num)/(10**multiplier) #finds the float after
    str_remainder = str(remainder)
    if trunc != 0:
        str_remainder = remainder[:trunc+1] #truncates to trunc digits total
    return str_remainder+'e'+str(multiplier) #can also be print

相关问题 更多 >

    热门问题