- 1
wt change i hv to make in this code to get decimal output frm binary
def decToBinConversion(no, precision): binary = "" IntegralPart = int(no) fractionalPart = no- IntegralPart #to convert an integral part to binary equivalent while (IntegralPart): re = IntegralPart % 2 binary += str(re) IntegralPart //= 2 binary = binary[ : : -1] binary += '.' #to convert an fractional part to binary equivalent while (precision): fractionalPart *= 2 bit = int(fractionalPart) if (bit == 1) : fractionalPart -= bit binary += '1' else : binary += '0' precision -= 1 return binary no = float(input()) k = int(input()) print("Binary Equivalent:",decToBinConversion(no,k))
1 Answer
+ 1
well, another copy-paste from another source with another issue: the new lines characters and tabulations are gone ^^
you should reformat it (help you from the source wich should show the correct format) and see if it works ;P