Skip to content

Instantly share code, notes, and snippets.

@jackiekazil
Last active January 17, 2024 12:29
Show Gist options
  • Save jackiekazil/6201722 to your computer and use it in GitHub Desktop.
Save jackiekazil/6201722 to your computer and use it in GitHub Desktop.

Revisions

  1. jackiekazil revised this gist Aug 24, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion rounding_decimals.md
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    ## How do I round to 2 decimals?

    In python, you have floats and decimals that can be rounded. If you care about the accuracy of rounding, use decimal type. If you use floats, you will have (issues with accuracy)[http://docs.python.org/2/tutorial/floatingpoint.html#tut-fp-issues].
    In python, you have floats and decimals that can be rounded. If you care about the accuracy of rounding, use decimal type. If you use floats, you will have [issues with accuracy](http://docs.python.org/2/tutorial/floatingpoint.html#tut-fp-issues).

    All the examples use demical types, except for the original value, which is automatically casted as a float.

  2. jackiekazil revised this gist Aug 24, 2013. 1 changed file with 4 additions and 2 deletions.
    6 changes: 4 additions & 2 deletions rounding_decimals.md
    Original file line number Diff line number Diff line change
    @@ -45,7 +45,7 @@ Output: 2.29



    ### 3: Round decimal by setting precision"
    ### 3: Round decimal by setting precision

    ```python
    # If you use deimcal, you need to import
    @@ -65,4 +65,6 @@ Output: 2.29

    In example 3, if we set the prec to 2, then we would have 2.3. If we set to 6, then we would have 2.28571.

    ** Which approach is best? They are all viable. I am a fan of the second option, because it offers the most control. If you have a very specific use case (i.e. 2010 WMATA practice rounding habits of up and down to the .05 depending on the fare), you may have to customize this part in your code. **
    #### Solution
    **Which approach is best? They are all viable.**
    I am a fan of the second option, because it offers the most control. If you have a very specific use case (i.e. 2010 WMATA practice rounding habits of up and down to the .05 depending on the fare), you may have to customize this part in your code.
  3. jackiekazil revised this gist Aug 24, 2013. 1 changed file with 7 additions and 14 deletions.
    21 changes: 7 additions & 14 deletions rounding_decimals.md
    Original file line number Diff line number Diff line change
    @@ -1,25 +1,17 @@
    ## How do I round to 2 decimals?

    What a great a question!!
    In python, you have floats and decimals that can be rounded. If you care about the accuracy of rounding, use decimal type. If you want to understand float type rounding issues, read this:
    http://docs.python.org/2/tutorial/floatingpoint.html#tut-fp-issues
    In python, you have floats and decimals that can be rounded. If you care about the accuracy of rounding, use decimal type. If you use floats, you will have (issues with accuracy)[http://docs.python.org/2/tutorial/floatingpoint.html#tut-fp-issues].

    All the examples use demical
    All the examples use demical types, except for the original value, which is automatically casted as a float.

    The format of this file is broken into a few different approaches.
    each approach can be run independently
    It is separated by "#################################################"
    You would not normally format way. This is just to make it digestable.
    To set the context of what we are working with, let's start with an original value.

    To set the context of what we are working with.

    ### Original Value
    #### Original Value
    ```python
    print 16.0/7
    ```
    Output: 2.2857142857142856


    ### 1: Round decimal using round()

    ```python
    @@ -71,5 +63,6 @@ print output
    ```
    Output: 2.29

    If we set the prec to 2, then we would have 2.3
    If we set to 6, then we would have 2.28571
    In example 3, if we set the prec to 2, then we would have 2.3. If we set to 6, then we would have 2.28571.

    ** Which approach is best? They are all viable. I am a fan of the second option, because it offers the most control. If you have a very specific use case (i.e. 2010 WMATA practice rounding habits of up and down to the .05 depending on the fare), you may have to customize this part in your code. **
  4. jackiekazil renamed this gist Aug 24, 2013. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  5. jackiekazil revised this gist Aug 24, 2013. 1 changed file with 34 additions and 28 deletions.
    62 changes: 34 additions & 28 deletions rounding_decimals.py
    Original file line number Diff line number Diff line change
    @@ -1,27 +1,28 @@
    # Q: How do I round to 2 decimals?
    ## How do I round to 2 decimals?

    # What a great a question!!
    # In python, you have floats and decimals that can be rounded.
    # If you care about the accuracy of rounding, use decimal type.
    # If you want to understand float type rounding issues, read this:
    # http://docs.python.org/2/tutorial/floatingpoint.html#tut-fp-issues
    What a great a question!!
    In python, you have floats and decimals that can be rounded. If you care about the accuracy of rounding, use decimal type. If you want to understand float type rounding issues, read this:
    http://docs.python.org/2/tutorial/floatingpoint.html#tut-fp-issues

    # All the examples use demical
    All the examples use demical

    # The format of this file is broken into a few different approaches.
    # each approach can be run independently
    # It is separated by "#################################################"
    # You would not normally format way. This is just to make it digestable.
    The format of this file is broken into a few different approaches.
    each approach can be run independently
    It is separated by "#################################################"
    You would not normally format way. This is just to make it digestable.

    # To set the context of what we are working with.
    print "Original value"
    To set the context of what we are working with.

    ### Original Value
    ```python
    print 16.0/7
    # 2.2857142857142856
    ```
    Output: 2.2857142857142856


    #################################################
    print "1: Round decimal using round()"
    #################################################
    ### 1: Round decimal using round()

    ```python
    from decimal import Decimal

    # First we take a float and convert it to a decimal
    @@ -30,12 +31,13 @@
    # Then we round it to 2 places
    output = round(x,2)
    print output
    # 2.29
    ```
    Output: 2.29


    #################################################
    print "2: Round decimal with super rounding powers"
    #################################################
    ### 2: Round decimal with super rounding powers

    ```python
    from decimal import Decimal, ROUND_HALF_UP
    # Here are all your options for rounding:
    # This one offers the most out of the box control
    @@ -46,12 +48,14 @@
    output = Decimal(our_value.quantize(Decimal('.01'), rounding=ROUND_HALF_UP))

    print output
    # 2.29
    ```
    Output: 2.29

    #################################################
    print "3: Round decimal by setting precision"
    #################################################


    ### 3: Round decimal by setting precision"

    ```python
    # If you use deimcal, you need to import
    from decimal import getcontext, Decimal

    @@ -64,6 +68,8 @@
    # Your output will return w/ 6 decimal places, which
    # we set above.
    print output
    # 2.29
    # If we set the prec to 2, then we would have 2.3
    # If we set to 6, then we would have 2.28571
    ```
    Output: 2.29

    If we set the prec to 2, then we would have 2.3
    If we set to 6, then we would have 2.28571
  6. jackiekazil revised this gist Aug 13, 2013. 1 changed file with 35 additions and 31 deletions.
    66 changes: 35 additions & 31 deletions rounding_decimals.py
    Original file line number Diff line number Diff line change
    @@ -1,9 +1,11 @@
    # Q: How do I round to 2 decimals?

    # What a great a question!!
    # There are two different ways depending on
    # whether your number is a float type or a decimal type.
    # If you care about the accuracy of rounding, use decimal type.
    # In python, you have floats and decimals that can be rounded.
    # If you care about the accuracy of rounding, use decimal type.
    # If you want to understand float type rounding issues, read this:
    # http://docs.python.org/2/tutorial/floatingpoint.html#tut-fp-issues

    # All the examples use demical

    # The format of this file is broken into a few different approaches.
    @@ -12,54 +14,56 @@
    # You would not normally format way. This is just to make it digestable.

    # To set the context of what we are working with.
    # 16/7 equals 2.2857142857142856
    print "Original value"
    print 16.0/7
    # 2.2857142857142856

    #################################################
    # EXAMPLE 1: Round decimal by setting precision
    print "1: Round decimal using round()"
    #################################################

    # If you use deimcal, you need to import
    import decimal

    # Set the precision.
    decimal.getcontext().prec = 3

    # Execute 1/7, however cast both numbers as decimals
    output = decimal.Decimal(16.0/7)

    # Your output will return w/ 6 decimal places, which
    # we set above.
    print output
    # 2.29
    # If we set the prec to 2, then we would have 2.3
    # If we set to 6, then we would have 2.28571

    #################################################
    # EXAMPLE 2: Round decimal using round()
    #################################################

    import decimal
    from decimal import Decimal

    # First we take a float and convert it to a decimal
    x = decimal.Decimal(16.0/7)
    x = Decimal(16.0/7)

    # Then we round it to 2 places
    output = round(x,2)
    print output
    # 2.29

    #################################################
    # EXAMPLE 3: Round decimal with super rounding powers
    print "2: Round decimal with super rounding powers"
    #################################################

    # from library import moduleA, moduleB
    # This works the same way as import library & later saying library.moduleA
    from decimal import Decimal, ROUND_HALF_UP
    # Here are all your options for rounding:
    # This one offers the most out of the box control
    # ROUND_05UP ROUND_DOWN ROUND_HALF_DOWN ROUND_HALF_UP
    # ROUND_CEILING ROUND_FLOOR ROUND_HALF_EVEN ROUND_UP

    our_value = Decimal(16.0/7)
    output = Decimal(our_value.quantize(Decimal('.01'), rounding=ROUND_HALF_UP))

    print output
    print output
    # 2.29

    #################################################
    print "3: Round decimal by setting precision"
    #################################################

    # If you use deimcal, you need to import
    from decimal import getcontext, Decimal

    # Set the precision.
    getcontext().prec = 3

    # Execute 1/7, however cast both numbers as decimals
    output = Decimal(16.0)/Decimal(7)

    # Your output will return w/ 6 decimal places, which
    # we set above.
    print output
    # 2.29
    # If we set the prec to 2, then we would have 2.3
    # If we set to 6, then we would have 2.28571
  7. jackiekazil renamed this gist Aug 10, 2013. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  8. jackiekazil created this gist Aug 10, 2013.
    65 changes: 65 additions & 0 deletions gistfile1.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,65 @@
    # Q: How do I round to 2 decimals?

    # What a great a question!!
    # There are two different ways depending on
    # whether your number is a float type or a decimal type.
    # If you care about the accuracy of rounding, use decimal type.
    # All the examples use demical

    # The format of this file is broken into a few different approaches.
    # each approach can be run independently
    # It is separated by "#################################################"
    # You would not normally format way. This is just to make it digestable.

    # To set the context of what we are working with.
    # 16/7 equals 2.2857142857142856

    #################################################
    # EXAMPLE 1: Round decimal by setting precision
    #################################################

    # If you use deimcal, you need to import
    import decimal

    # Set the precision.
    decimal.getcontext().prec = 3

    # Execute 1/7, however cast both numbers as decimals
    output = decimal.Decimal(16.0/7)

    # Your output will return w/ 6 decimal places, which
    # we set above.
    print output
    # 2.29
    # If we set the prec to 2, then we would have 2.3
    # If we set to 6, then we would have 2.28571

    #################################################
    # EXAMPLE 2: Round decimal using round()
    #################################################

    import decimal

    # First we take a float and convert it to a decimal
    x = decimal.Decimal(16.0/7)

    # Then we round it to 2 places
    output = round(x,2)
    print output
    # 2.29

    #################################################
    # EXAMPLE 3: Round decimal with super rounding powers
    #################################################

    # from library import moduleA, moduleB
    # This works the same way as import library & later saying library.moduleA
    from decimal import Decimal, ROUND_HALF_UP
    # Here are all your options for rounding:
    # ROUND_05UP ROUND_DOWN ROUND_HALF_DOWN ROUND_HALF_UP
    # ROUND_CEILING ROUND_FLOOR ROUND_HALF_EVEN ROUND_UP

    our_value = Decimal(16.0/7)
    output = Decimal(our_value.quantize(Decimal('.01'), rounding=ROUND_HALF_UP))

    print output