аватар question@mail.ru · 01.01.1970 03:00

Fixed number of signs after aim in Python

Is there an analogue of the function & nbsp; toofixed () in JS? I need something like this:

   & gt; & gt;  a =  12.3456789    & gt; & gt; & gt;  a.tofixed ( 2 )   '' 12.35 '   & gt; & gt; & gt;  a.tofixed ( 0 )   '12'    & gt; & gt; & gt;  b =  12.000001    & gt; & gt; & gt;  b.tofixed ( 3 )   '' 12,000 '   
аватар answer@mail.ru · 01.01.1970 03:00

analogue in Python 3.6+:

   de   numobj, digits =  0  ):   retu   f ""  {numobj:. {digits}  f} ""      

Example:

   & gt; & gt; & gt;  numobj =  12345.6789    & gt; & gt; & gt;  Tofixed (numobj)   '' 12346 '   & gt; & gt; & gt;  TOFIXED (numobj,  1 )   '' 12345.7 '   & gt; & gt; & gt;  Tofixed (numobj,  6 )   '' 12345.678900 '   & gt; & gt; & gt;  Tofixed ( 1.23e+20 ,  2 )   '' 123000000000000000000.00 '   & gt; & gt; & gt;  TOFIXED ( 1.23E-10 ,  2 )   '' 0.00 '   & gt; & gt;  Tofixed ( 2.34 ,  1 )   '' 2.3 '   & gt; & gt;  TOFIXED ( 2.35 ,  1 )   '' 2.4 '   & gt; & gt;  TOFIXED (- 2.34 ,  1 )   ''-2.3 '                                       

Latest

Similar