Python逗号并除以整数

时间:2015-03-17 16:11:38

标签: python int comma divide

def SetHP(self, hpPercentage, curHP, maxHP):
    if not self.hpGauge.IsShow():
        self.SetSize(200 + 7*self.nameLength, 70)
        self.hpGauge.Show()
        self.UpdatePosition()

    self.hpGauge.SetPercentage(hpPercentage, 100)

    strCurHP = str(curHP)
    strMaxHP = str(maxHP)
    self.broadCastHP.SetText(strCurHP + " / " + strMaxHP)

示例输出为:8993 / 18782

我看到了类似的一些问题,但所有问题都是关于"浮动"。

我想这样做这些整数:

8,9K / 18,7K

什么是"适当的"这样做的方法?

1 个答案:

答案 0 :(得分:0)

尝试此功能:

def HPformat(str):
    if len(str)==5:
        newstr=str[0]+str[1] + ','+ str[2]  + 'K'
        return newstr
    elif len(str)==4:
        newstr=str[0]+','+ str[1] + 'K'
        return newstr

用函数替换最后一行代码:

def SetHP(self, hpPercentage, curHP, maxHP):
    if not self.hpGauge.IsShow():
        self.SetSize(200 + 7*self.nameLength, 70)
        self.hpGauge.Show()
        self.UpdatePosition()
    self.hpGauge.SetPercentage(hpPercentage, 100)

    strCurHP = str(curHP)
    strMaxHP = str(maxHP)
    self.broadCastHP.SetText(HPformat(strCurHP) + " / " + HPformat(strMaxHP))

此外,如果您不想添加新功能,您可以这样做:

def SetHP(self, hpPercentage, curHP, maxHP):
    if not self.hpGauge.IsShow():
        self.SetSize(200 + 7*self.nameLength, 70)
        self.hpGauge.Show()
        self.UpdatePosition()
    self.hpGauge.SetPercentage(hpPercentage, 100)

    strCurHP = str(curHP)
    strMaxHP = str(maxHP)

    newCurHP = strCurHP[0] + ',' + strCurHP [1] + 'K'
    newMaxHP = strMaxHP[0] + strMaxHP[1] + ',' + strMaxHP[2]  + 'K'

    self.broadCastHP.SetText(newCurHP + " / " + newMaxHP)