Last active
January 17, 2024 12:29
-
Star
(123)
You must be signed in to star a gist -
Fork
(23)
You must be signed in to fork a gist
-
-
Save jackiekazil/6201722 to your computer and use it in GitHub Desktop.
Revisions
-
jackiekazil revised this gist
Aug 24, 2013 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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). All the examples use demical types, except for the original value, which is automatically casted as a float. -
jackiekazil revised this gist
Aug 24, 2013 . 1 changed file with 4 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -45,7 +45,7 @@ Output: 2.29 ### 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. #### 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. -
jackiekazil revised this gist
Aug 24, 2013 . 1 changed file with 7 additions and 14 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,25 +1,17 @@ ## 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]. All the examples use demical types, except for the original value, which is automatically casted as a float. To set the context of what we are working with, let's start with an 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 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. ** -
jackiekazil renamed this gist
Aug 24, 2013 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
jackiekazil revised this gist
Aug 24, 2013 . 1 changed file with 34 additions and 28 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,27 +1,28 @@ ## 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 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. ### Original Value ```python print 16.0/7 ``` Output: 2.2857142857142856 ### 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 ``` Output: 2.29 ### 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 ``` Output: 2.29 ### 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 ``` 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 -
jackiekazil revised this gist
Aug 13, 2013 . 1 changed file with 35 additions and 31 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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!! # 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. print "Original value" print 16.0/7 # 2.2857142857142856 ################################################# print "1: Round decimal using round()" ################################################# from decimal import Decimal # First we take a float and convert it to a decimal x = Decimal(16.0/7) # Then we round it to 2 places output = round(x,2) print output # 2.29 ################################################# print "2: Round decimal with super rounding powers" ################################################# 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 # 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 -
jackiekazil renamed this gist
Aug 10, 2013 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
jackiekazil created this gist
Aug 10, 2013 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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