4

I want to convert the number 0.054000 in a str, but when I write srt(0.054000) I get '0.054'. I need to get '0.054000'. How can I do it?

I have a data file with numbers as my example (0.054000). I need to count the digits of each number. I don't know how to read that number in a way that I count seven digits, for instance.

  • 3
    >>> a = 0.054 >>> "{:08f}".format(a) gives '0.054000' –  May 16 '16 at 01:54
  • 1
    @DanPatterson shouldn't it be `"{:06f}".format(a)`? – Anzel May 16 '16 at 02:02
  • should have used !r ... >>> "{!r:<06}".format(a) gives '0.0540' While >>> "{!r:<08}".format(a) '0.054000' !s doesnt worry as much –  May 16 '16 at 02:17
  • The float never tracked significant figures in the first place. If you want to retain that information, you'll need to track it yourself or find a library that does it. – user2357112 May 16 '16 at 02:23
  • 1
    I think this is a duplicate of [Nicely representing a floating-point number in python](http://stackoverflow.com/questions/2663612/nicely-representing-a-floating-point-number-in-python), with the code from the accepted answer `f(0.0056, 5) -> '0.0056000'` – Tadhg McDonald-Jensen May 16 '16 at 02:35
  • I have a data file with numbers as my example (0.054000). I need to count the digits of each number, so your idea doesn't work for me. I don't know how to read that number in a way that I count seven digits, for instance. – Danny Alzate May 16 '16 at 02:44
  • 3
    @DanielaAlzate: Read it as a string, then. Don't convert it to a float in the first place. – user2357112 May 16 '16 at 03:10

2 Answers2

0

I think that Dan Patterson's method is the only way to do it reliably - python makes no differentiation between .0054 and .054000: e.g.

>>> .0054 is .0054000
True

Thus you will probably have to simply specify the number of digits you have in sig figs, either using his method or (str(.0054000) + "0"*number_of_sig_figs).

computergorl
  • 133
  • 1
  • 8
  • why would you ever use `(str(.0054000) + "000")` in actual code? That only adds 3 `0` at the end of the number, it has nothing to do with significant digits. – Tadhg McDonald-Jensen May 16 '16 at 02:19
  • @TadhgMcDonald-Jensen That didn't make much sense, sorry. What I meant is that you would have to keep track of how many sig figs you have and just add those zeroes back on at the end. – computergorl May 16 '16 at 02:21
  • but given that `.0054000` should have `5` significant digits how would you actually go about figuring out that you should add *3* extra zeros to the end? – Tadhg McDonald-Jensen May 16 '16 at 02:23
0

A format specifier starts with a colon and then may contain any of the terms shown in brackets in the following (each of the terms is optional)

: [[fill]align] [sign] [#] [0] [width] [,] [.precision] [type]

A brief description of the [.precision] is provided below.

.precision: Maximum number of characters for strings (integer); number of digits of precision for floats. For f, F, e, and E type specifiers this is the number of digits to the right of the decimal point.

We can use this to specify the precision of our float value:

a=0.540000
print("{:06f}".format(a))

This gives the desired output:

0.540000

Hope this was helpful!

Anshika Singh
  • 994
  • 12
  • 20