Hello, how can I suppress the decimal places for (only those) numbers whos decimal places are zero (0)?
Example: #### CODE #### In [1]: m = 2.0 In [2]: n = 2.56789080 In [3]: n_format = '%.4f' %n In [4]: n_format Out[4]: '2.5679' In [5]: m_format = '%.4f' %m In [6]: m_format Out[6]: '2.0000' ### END #### I would like to have the "m_format" to be formatted like "2" and the n_format like "2.5679". How can I achive this? A g with "2.4" should return "2.4" and not "2.4000". Basically, I am looking for a way to eliminate the decimal places that are zero (0). I tried this humble function before appling the formatting and it works well with the numbers: ### CODE ### def cut_decimals(float): """ input: floating number output: number as string with zero decimals removed """ #print float-int(float) #print '%.4f' %float if float-int(float) != 0: number = '%.4f' %float number = number.replace('0.','X.') number = number.replace('0','') number = number.replace('X.','0.') else: number = '%.0f' %float return number n = 2.0 m = 2.5678908 g = 2.4 h = 1.45 i = 0.67 numbers = [n, m, g, h, i] for i in numbers: i_f = cut_decimals(i) print i_f ### END ### ### OUTPUT ### %run ./test_number.py 2 2.5679 2.4 1.45 0.67 ### END ### Is there any more efficient solution using string formatting only? Thanks in adavance. Kind regards, Timmie _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor