Skip to content

fix it. byteToPercent() and PercentToByte(). #38

@pwnbit

Description

@pwnbit

Existing code

def byteToPercent(byte):
    ...
    return int((byte * 100)/255)

def percentToByte(percent):
    ...
    return int((percent * 255)/100)

Modified code
Add Round() to Return.

def byteToPercent(byte):
    ...
    return int(round(byte * 100/255, 0))

def percentToByte(percent):
    ...
    return int(round(percent * 255/100, 0))

Comparison of results.

Code for comparison.

print(f"|{'before code':^22}|{'after code':^22}|")
print(f"|% to byte | byte to % | % to byte | byte to %|")
print("-"*47)
for i in range(0, 101):
    a = percentToByte(i)
    b = byteToPercent(a)

    c = ptb(i)
    d = btp(c)

    print(f"|{a:^10}|{b:^11}|{c:^11}|{d:^10}|")

Modifying the code increases accuracy.
이미지 20210305_222058
The modified code represents exactly 0 ~ 100, 0 ~ 255.

Ex)

input = 70
byte1 = percentToByte(input)
per1 = byteToPercent(byte1)

byte2 = percentToByte_fixed(input)
per2 = byteToPercent_fixed(byte2)
print(byte1, per1, byte2, per2)
---------
Result :
178 69 178 70

Why did I enter 70, but it becomes 69?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions