0

I have a python code that when given a small number between 0 and 1 doesn't print it fully, but 4.43017984825e-7 for example,how do I make the code shows the whole number with all zeroes?

this was my try:

number="4.43017984825e-7"
result=number.find("e")
new=list(number)
last=int(new[-1])
print(last)
del new[13:16]

print(new)
pricee=(''.join(new))
print(pricee)
price=float(pricee)*10**-(last)

print(price)

Note: the number changes everytime, so I want it to be applicable for all numbers.

  • 3
    Could you please provide the code? What have you tried? – puncher Aug 06 '22 at 12:36
  • number="4.43017984825e-7" result=number.find("e") new=list(number) last=int(new[-1]) print(last) del new[13:16] print(new) pricee=(''.join(new)) print(pricee) price=float(pricee)*10**-(last) print(price) I tried this :) – Badis Kerdellou Aug 06 '22 at 12:42
  • Does this answer your question? [How to suppress scientific notation when printing float values?](https://stackoverflow.com/questions/658763/how-to-suppress-scientific-notation-when-printing-float-values) – puncher Aug 06 '22 at 12:46
  • Does this answer your question? [Print a float number in normal form, not exponential form / scientific notation](https://stackoverflow.com/questions/7801586/print-a-float-number-in-normal-form-not-exponential-form-scientific-notation) – Ohad Sharet Aug 06 '22 at 12:47

1 Answers1

2

You can probably accomplish what you want with fixed-point formatting.

>>> x=4.43017984825e-7
>>> print(x)
4.43017984825e-07
>>> print(f"{x:20.18f}")
0.000000443017984825

The 20 in that format tells the full width you want, while the 18 tells the number of decimals.

Now, this is fairly specific to this number, you'll have to pick the right length and number of decimals for your actual application.

Expanding on the suggestion from @MostafaFarzán: you can use log10 to adjust that fixed point formatting to any number:

x = <some float>
significant_digits = 8
decimals=max(0, int(-log10(x) + significant_digits))
print(f"%.{decimals}f" % x)

or, more concisely but harder to read:

print(f"%.{max(0, int(-log10(x) + 8))}f" % x)
joanis
  • 10,635
  • 14
  • 30
  • 40
  • I need it to be applicable for all numbers – Badis Kerdellou Aug 06 '22 at 12:43
  • 2
    Please give multiple examples of numbers and what you want as output. You need scientific notation if you want to be able to display *any* floating point number. – joanis Aug 06 '22 at 12:44
  • 1
    You can use `log10` function to calculate the width of the string to be produced. For example, this will always print the float with the leading zeros and 5 more decimal places: `f'%.{int(-log10(x)+5)}f' % x` – Mostafa Farzán Aug 06 '22 at 12:46
  • @MostafaFarzán Just tested, and this works very nicely, but it breaks when log10(x) >= 6, so a little bit of tweaking (with min or max) will be needed. – joanis Aug 06 '22 at 12:54
  • @joanis Yeah, I just provided it as a hint for the OP, with numbers between 0 and 1 in mind. It can surely be extended to work with a wider range of numbers, based on the behavior one needs. – Mostafa Farzán Aug 06 '22 at 13:07