Python Forum
Force calculation result as decimal
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Force calculation result as decimal
#1
Hi folks,

I come here to ask you a quick question

I am a noob in python and I have a so simple problem. I looked at several python documentations, but I think my poor basic knowlegde of python doesn't permit me to find what I am looking for....so I came here^^

I am not developping a python script, but simply using the calculation feature of ptyhon, which is more powerfull than the one on script shell.
I'm doing it by calling "python -c" from linux CLI

#python -c "print 0.0000006 + 0.0000008"
1.4e-06
question is : how do I force python to have a result in decimal (example 0.xxxxxxxxxx)

Thanks a lot in advance
Reply
#2
If you want a number formatted a certain way, then you probably want to use string formatting. Or the decimal module, depending on how exact you want it:
C:\Users\nilamo>python -c "print('{0:.10f}'.format(0.0000006 + 0.0000008))"
0.0000014000

C:\Users\nilamo>python -c "from decimal import Decimal; print(Decimal(0.0000006 + 0.0000008))"
0.000001399999999999999936647356556240762159859514213167130947113037109375

C:\Users\nilamo>python -c "from decimal import Decimal; print(Decimal(0.0000006) + Decimal(0.0000008))"
0.000001399999999999999936647356556
Reply
#3
Hi Nilamo,

Thanks a lot for your answer.

Your first command works fine, for the 2 others it returns :

# python -c "from decimal import Decimal; print(Decimal(0.0000006) + Decimal(0.0000008))"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/usr/lib64/python2.6/decimal.py", line 649, in __new__
    "First convert the float to a string")
is there no command to simply display the result that automatically stick to the number with the higher number of digit?

thx a lot
Reply
#4
To avoid errors of floating point object representation in memory, it is better to pass strings to Decimal class, e.g.
python -c "from decimal import Decimal; print(Decimal('0.0000006') + Decimal('0.0000008'))". This likely should work in python2.6.
@vercetty92, why did you are still using Python 2.6? Constructor of Decimal class in current versions of Python, e.g. Python 3.7, could process floats directly (without errors as in your example).
Reply
#5
Hi Scidam,
this solution is perfect to me thx a lot dude!

I am using python 2.6 because this is the version redhat provides by defaut (2.6 on el6 & 2.7 on el7). Of course we can enable the software-collection repository and install python3, but we never really had this need (the destination server is not really a development server so we avoid to install unecessary thing^^)

thx
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Need an alternative to brute force optimization loop jmbonni 5 4,395 Jun-05-2024, 02:21 AM
Last Post: stevejobb
  Solving an equation by brute force within a range alexfrol86 3 5,136 Aug-09-2022, 09:44 AM
Last Post: Gribouillis
  force a program to exit ? Armandito 3 6,840 May-12-2022, 04:03 PM
Last Post: Gribouillis
  best way to force an exception Skaperen 2 3,370 Oct-21-2020, 05:59 AM
Last Post: Gribouillis
  testing for Decimal w/o importing decimal every time Skaperen 7 7,281 May-06-2019, 10:23 PM
Last Post: Skaperen
  Password Brute Force 2skywalkers 9 9,523 Oct-18-2018, 02:35 PM
Last Post: buran
  Brute Force Password Guesser 2skywalkers 1 4,489 Oct-05-2018, 08:04 PM
Last Post: ichabod801
  Brute Force Pad Lock Guesser RedSkeleton007 4 6,516 Mar-03-2018, 07:42 AM
Last Post: RedSkeleton007
  Speeding up Brute force password guesser Gamervote 5 9,670 Jul-20-2017, 02:52 PM
Last Post: nilamo
  brute force password from list petru 10 17,699 Apr-04-2017, 02:08 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020