Python 10 or two binary conversion problem with decimals

Source: Internet
Author: User
Tags pow

1 " "2 decimal, binary: Btod3 integer part: A multiplied by 2 of the n-th square (the number of integers after n:a)4 Fractional part: A Times 2 's-N-Square (N:a is a few decimal points)5 decimal --binary Dtob6 Integer part: Short division (except for two-method, reverse output)7 Fractional part: By two rounding method, 0 .... Positive output derived8     9 Parameter n is a string type in a functionTen " " One #decimal in binary A defBtod (n, pre = 4): -S_int =N -S_float ="' theTotal_int, total_float =0, 0 -     if '.' inchN: -S_int, s_float = N.split ('.') -          +Li =Len (s_int) -      forCinchS_int: +Li = li-1 ATotal_int = total_int + int (c) *pow (2, Li) at          -LF =0 -      forCinchs_float: -LF = LF + 1 -Total_float = Total_float +int (c) *pow (2,-LF) -          in     Print("{} conversion to decimal is {:. {} F}". Format (N,total_int +Total_float, pre)) -  to #decimal --Binary + defDtob (n, pre = 4): -Num_int =Int (eval (n)) theNum_float = eval (n)-Num_int *T_int, t_float ="',"' $     if '.' inchN:Panax NotoginsengS_int, s_float = N.split ('.') -  the      whilenum_int!=0: +A = num_int% 2 AT_int = T_int +Str (a) theNum_int = NUM_INT//2 +  -      whileNum_float! =0: $b = num_float*2 $T_float = T_float +str (int (b)) -Num_float = B-Int (b) -   the     #print (t_int[::-1] + '. ' + t_float[:]) -     Print("{} conversion to binary is {:. {} F}". Format (N,eval (t_int[::-1) +'.'+t_float[:]) (pre) )Wuyi  the  -A = input ("Please enter 0 (convert to decimal) or enter 1 (convert to binary):") Wu ifA = ='0': -      whileTrue: Aboutn = input ("Please enter a binary number:") $Flag = 1 -          forCinchN: -             ifc! ='1'  andc! ='0': -                 Print("input Error, please re-enter") AFlag =0 +                  Break the         ifflag: -              Break $Pre = Int (input ("Please enter a few decimals to keep:")) the Btod (N,pre) the elifA = ='1': the      whileTrue: then = input ("Please enter a decimal number:") -Flag = 1 in          forCinchN: the             ifC <='0' orC >='9': the                 Print("input Error, please re-enter") AboutFlag =0 the                  Break the         ifflag: the              Break +Pre = Int (input ("Please enter a few decimals to keep:")) - Dtob (N,pre) the Else:Bayi     Print("Invalid input")

Python 10 or two binary conversion problem with decimals

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.