Skip to content
Advertisement

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)

Example output is: 8993 / 18782

I see some questions like that, but all of them was about “float”.

I want to make these integers like that:

8,9K / 18,7K

What is the “proper” way to do that?

Advertisement

Answer

Try this function:

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

And replace your final line of code with the function:

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))

Also, if you don’t want to add a new function you could just do:

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)
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement