Floating point issue

Cousin Stanley cousinstanley at gmail.com
Fri Apr 17 11:14:38 EDT 2020


Aakash Jana wrote:

> I am calculating couple of ratios and according to the problem 
> there must be a 6 decimal precision. 
> 
> But when I print the result I only get one 0.
>
> E.g:- 2 / 5 = 0.400000 but I am only getting 0.4

You might try using a  ' '.format  string ....

ratios = { 
 '1/8'  :  1/8 , 
 '1/5'  :  1/5 ,  
 '1/4'  :  1/4 , 
 '1/3'  :  1/3 , 
 '2/5'  :  2/5 , 
 '5/9'  :  5/9 , 
 '2/3'  :  2/3 , 
 '4/5'  :  4/5 , 
 '7/8'  :  7/8 , 
 '9/5'  :  9/5 , 
 '22/7' :  22/7 }

print()
 
for fraction , ratio in ratios.items() : 

    print( '  {:4s}  {:.6f} '.format( fraction , ratio ) )









-- 
Stanley C. Kitching
Human Being
Phoenix, Arizona



More information about the Python-list mailing list