-2

How can I write a function that show how many decimal places each float-3 value has. How would I be able to get that?

float1= 4.9778
float2 = 6.9987475673579
float3= 4567.1     

Expected output:

4
13
1
tony selcuk
  • 709
  • 3
  • 11
  • 2
    First, read over [Is floating point math broken?](https://stackoverflow.com/q/588004/354577). You probably want to use [decimal](https://docs.python.org/library/decimal.html) values instead of floating point for this kind of thing. – ChrisGPT was on strike Jun 15 '21 at 21:17
  • 3
    A float has no "number of decimal places". If you want that, use the [decimal](https://docs.python.org/3/library/decimal.html) module. – mkrieger1 Jun 15 '21 at 21:17
  • what do you mean by the decimal module? – tony selcuk Jun 15 '21 at 21:18
  • Unless the value is a multiple of a power of two (with a positive or negative exponent), the fractional part will repeat. And in any case, it wouldn't be what you want. For example, `0.1` is a repeating value in binary, but it cannot be represented exactly in floating point, so you'd get some undesirable approximation to it. – Tom Karzes Jun 15 '21 at 21:19
  • 1
    Please repeat [on topic](https://stackoverflow.com/help/on-topic) and [how to ask](https://stackoverflow.com/help/how-to-ask) from the [intro tour](https://stackoverflow.com/tour). “Show me how to solve this coding problem” is not a Stack Overflow issue. We expect you to make an honest attempt, and *then* ask a *specific* question about your algorithm or technique. Stack Overflow is not intended to replace existing documentation and tutorials. If you *do* develop a clear problem specification that makes sense, your question is still off topic. – Prune Jun 15 '21 at 21:19
  • 3
    Don't ask what we mean: do your research to learn the terminology. See [How much research?](https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users). – Prune Jun 15 '21 at 21:19
  • @tonyselcuk In the Python standard library there is the decimal library - https://docs.python.org/3/library/decimal.html#module-decimal – Tony Suffolk 66 Jun 15 '21 at 21:29

1 Answers1

0

You should not use float as a variable name. You will be unable to use it if you want to create a float variable. Your problem is easy to solve. Just use this:

num = 124.178
tmp = len(str(num).split(".")[1])
print(tmp)
jaumes5
  • 36
  • 2
  • 1
    Change `[0]` to `[1]` – Corralien Jun 15 '21 at 21:25
  • Thanks! That was an embarrassing error XD – jaumes5 Jun 15 '21 at 21:28
  • 2
    The problem here is that you collide headlong with the precision of the floating point system, and you find that floating point numbers wont always resolve into nice numbers of decimal places. – Tony Suffolk 66 Jun 15 '21 at 21:31
  • 3
    This gives unexpected results e.g. for `num = 0.00001`. – mkrieger1 Jun 15 '21 at 21:32
  • @mkrieger1that is entirely due to floating point numbers being pretty imprecise and how Python tries to convert small numbers to strings - for instance 0.0001 is represented as the string 1e-05 for example. – Tony Suffolk 66 Jun 15 '21 at 21:34
  • 2
    In that case it is due to how `str` works with some `float` values. – mkrieger1 Jun 15 '21 at 21:37
  • @mkrieger1 very true - but I wouldn't rely on floating point numbers if if i wanted to count decimal points. – Tony Suffolk 66 Jun 15 '21 at 21:38
  • 1
    Yes, you do not need to tell me that, I know. I was trying to point out a flaw in the solution that is proposed in this answer. – mkrieger1 Jun 15 '21 at 21:38
  • I agree that the float numbers are not the best since they are pretty imprecise. But if I convert the floats to Decimal (from the decimal package) the value is not going to be precise as it's based on a float. The problem it's that if we suppose that the numbers are float at the beginning there's no much else that we can do. I least nothing that I can think of ! – jaumes5 Jun 15 '21 at 22:03