Given a percent--for example, 5.43%--call its:
- numeric form -> 5.43
- decimal form -> 0.0543
The logic for converting between the two would be as follows:
input form output form operation
---------- ----------- ---------
numeric numeric multiply by 1.
decimal decimal multiply by 1.
numeric decimal multiply by 0.01
decimal numeric multiply by 100.
I'm interested in a more pythonic alternative to the following dict lookup. Because I'll be calling this conversion a number of times I'd prefer *not* to use logical operators. (I think...)
convert = {('num', 'num') : 1.,
('dec', 'dec') : 1.,
('num', 'dec') : 0.01,
('dec', 'num') : 100.
}
def converter(num, input_form='num', output_form='dec'):
return num * convert[(input_form, output_form)]
num = 5.43
print(converter(num))
Is this question too broad? Comment and let me know, and I'll try to hone in on what I'm looking for, but frankly I'm just interested in seeing other implementations. Basically, I currently have a class-based implementation where I want to establish a numeral and decimal form of self
at instantiation and then also use the function within methods as well.