Python逗号除以一个整数

Python comma and divide an integer

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

我看到一些类似的问题,但都是关于 "float"。

我想像这样生成这些整数:

8,9K / 18,7K

"proper" 方法是什么?

试试这个功能:

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)