import re def btc_to_sat(text): if not isinstance(text,str) or not re.fullmatch(r'[0-9]{1,8}(\.[0-9]{1,8})?',text): raise ValueError('invalid decimal') whole, _, fraction = text.partition('.') sat = int(whole)*100_000_000 + int(fraction.ljust(8,'0')) if not 0 < sat <= 21_000_000*100_000_000: raise ValueError('out of range') return sat if __name__ == '__main__': assert btc_to_sat('0.00000001') == 1 assert btc_to_sat('0.0012') == 120000 try: btc_to_sat('0.000000001') except ValueError: pass else: raise AssertionError('fractional satoshi accepted')