1

I have a floating point field in one of my form, consider it as field_x. Based on that field_x i have some computation. After all if field_x have n digits after decimal result also should have n digits.

For example: field_x = 0.00000001(n digits after decimal)

result = some calculations

if result = 22 i have to display it as 22.00000000(n digits after decimal)

len(str(number-int(number))[1:]) Gives the answer

**here the number can be 0.00101,0.110,0.787,etc

But for some values like 0.000001 its giving incorrect answer

PsPranav
  • 973
  • 7
  • 10
  • for your example the string is `1e-06` !! wrong method. – Jean-François Fabre May 04 '17 at 14:14
  • ys i know..thats why is asked for an answer – PsPranav May 04 '17 at 14:17
  • floats have an internal representation that may surprise you. So when entering 0.00001 to a `decimal.Decimal` object I get `0.000010000000000000000818030539140313095458623138256371021270751953125` ... difficult to answer the question. If the number is entered/read from a file, then use the string representation to compute your value, don't cast to float. – Jean-François Fabre May 04 '17 at 14:23
  • yup.........i tried....if its decimal.Decimal() its ok – PsPranav May 04 '17 at 14:26
  • can you help converting .00000001 to string exactly like this – PsPranav May 04 '17 at 14:27
  • 1
    sorry, that's not possible if you enter the data in a float due to loss of precision when converting your string to a float. Can you widen your problem. Where do the numbers come from? – Jean-François Fabre May 04 '17 at 14:34
  • @Jean-FrançoisFabre yes it is possible to find the minimal decimal fraction back even after converting to float http://stackoverflow.com/questions/18886012/convert-float-to-rounded-decimal-equivalent/18905753#18905753 – aka.nice May 04 '17 at 18:31
  • @PsP: What if the user enters "1.00200" in the form? How many digits after the decimal should that count as? If you need to count that as 5 digits after the decimal, then you'll need to get hold of the actual string entered into the form *before* it's converted to a `float` (or configure the form so that you receive a `Decimal` object rather than a `float`). Once you have a `float`, that information is not recoverable. – Mark Dickinson May 05 '17 at 08:02

1 Answers1

0

I have'nt found a proper answer for this issue anywhere. Either we need to use double precision which it is not possible in odoo or we have to convert to string and get the exponential length. I have chosen second one. Thank you

PsPranav
  • 973
  • 7
  • 10