MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / convert_si_prefix

Function convert_si_prefix

conversions/prefix_conversions.py:44–70  ·  view source on GitHub ↗

Wikipedia reference: https://en.wikipedia.org/wiki/Binary_prefix Wikipedia reference: https://en.wikipedia.org/wiki/International_System_of_Units >>> convert_si_prefix(1, SIUnit.giga, SIUnit.mega) 1000 >>> convert_si_prefix(1, SIUnit.mega, SIUnit.giga) 0.001 >>> convert_

(
    known_amount: float,
    known_prefix: str | SIUnit,
    unknown_prefix: str | SIUnit,
)

Source from the content-addressed store, hash-verified

42
43
44def convert_si_prefix(
45 known_amount: float,
46 known_prefix: str | SIUnit,
47 unknown_prefix: str | SIUnit,
48) -> float:
49 """
50 Wikipedia reference: https://en.wikipedia.org/wiki/Binary_prefix
51 Wikipedia reference: https://en.wikipedia.org/wiki/International_System_of_Units
52 >>> convert_si_prefix(1, SIUnit.giga, SIUnit.mega)
53 1000
54 >>> convert_si_prefix(1, SIUnit.mega, SIUnit.giga)
55 0.001
56 >>> convert_si_prefix(1, SIUnit.kilo, SIUnit.kilo)
57 1
58 >>> convert_si_prefix(1, 'giga', 'mega')
59 1000
60 >>> convert_si_prefix(1, 'gIGa', 'mEGa')
61 1000
62 """
63 if isinstance(known_prefix, str):
64 known_prefix = SIUnit[known_prefix.lower()]
65 if isinstance(unknown_prefix, str):
66 unknown_prefix = SIUnit[unknown_prefix.lower()]
67 unknown_amount: float = known_amount * (
68 10 ** (known_prefix.value - unknown_prefix.value)
69 )
70 return unknown_amount
71
72
73def convert_binary_prefix(

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected