PS039 – Decimal Library



The Aim


This aim of this session is to give you some familiarity with the decimal library for when you are dealing with decimal numbers, and want more control over them.



The Lesson


We know now that floating point numbers are not accurate, and we know how to use isclose() to check for numbers being close to each other, but we can take more control over floating point numbers by using the decimal library, should we want to do so. In this session we learn how to use the decimal library.



You can return here to play and pause the above video should you need to watch it again. Remember that you can watch the videos in full screen mode if you like, click the icon at the bottom right of the video to go to full screen mode, press the Esc key to return to normal windowed mode.




Points To Note


  • We use the decimal library when we want to take more control of floating point numbers
  • We might want to work with fewer or greater decimal places.
  • use Decimal() with a capital D, lower case won’t work.
  • To find out how many decimal places we are working to we can type getcontext() in to the left hand window.
  • getcontext().prec=x will set the number of decimal places to whatever value you want to use in the rest of your program.



Further Development

If you want to take your skills further then find out about the other things you can do with getcontext() such as deciding whether numbers round up or down.