Popular recipes tagged "decimal"http://code.activestate.com/recipes/tags/decimal/2016-10-10T20:40:48-07:00ActiveState Code RecipesGenerate a PDF cheat sheet for converting 0 to 255 between bin / oct / dec / hex (Python) 2016-10-10T20:40:48-07:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/580706-generate-a-pdf-cheat-sheet-for-converting-0-to-255/ <p style="color: grey"> Python recipe 580706 by <a href="/recipes/users/4173351/">Vasudev Ram</a> (<a href="/recipes/tags/binary/">binary</a>, <a href="/recipes/tags/conversion/">conversion</a>, <a href="/recipes/tags/decimal/">decimal</a>, <a href="/recipes/tags/hexadecimal/">hexadecimal</a>, <a href="/recipes/tags/octal/">octal</a>, <a href="/recipes/tags/pdfwriter/">pdfwriter</a>, <a href="/recipes/tags/pdf_generation/">pdf_generation</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/utility/">utility</a>, <a href="/recipes/tags/xtopdf/">xtopdf</a>). </p> <p>This recipe shows how to generate a PDF cheat sheet, that contains a table for conversion of the numbers 0 to 255 (the numbers that can fit in one byte) between binary, octal, decimal and hexadecimal representations. The table has four columns, one for each of those bases, and 256 rows, for the numbers 0 to 255.</p> <p>TO use the table, you can look for a number, say in decimal, in the Dec(imal) column (or use the search function of your PDF viewer), then when you find it in some row, just look at the other 3 columns in that row, to find the value of that number in binary, octal and hexadecimal. And use the same procedure if starting with a number in any of the other three bases.</p> Continued fractions (Python) 2013-08-22T07:15:47-07:00Glenn Hutchingshttp://code.activestate.com/recipes/users/4175415/http://code.activestate.com/recipes/578647-continued-fractions/ <p style="color: grey"> Python recipe 578647 by <a href="/recipes/users/4175415/">Glenn Hutchings</a> (<a href="/recipes/tags/decimal/">decimal</a>, <a href="/recipes/tags/fractions/">fractions</a>). Revision 2. </p> <p>A class representing a <a href="https://en.wikipedia.org/wiki/Continued_fraction">continued fraction</a>.</p> Convert from decimal to base-n for any positive n less than or equal to 10 (Python) 2013-08-09T05:56:16-07:00Samuel James Ericksonhttp://code.activestate.com/recipes/users/4187478/http://code.activestate.com/recipes/578630-convert-from-decimal-to-base-n-for-any-positive-n-/ <p style="color: grey"> Python recipe 578630 by <a href="/recipes/users/4187478/">Samuel James Erickson</a> (<a href="/recipes/tags/binary/">binary</a>, <a href="/recipes/tags/decimal/">decimal</a>, <a href="/recipes/tags/octal/">octal</a>). </p> <p>Takes in any non-negative integer and converts to the desired base-n for 1&lt;=n&lt;=10.</p> Complex Methods to Compute Pi in Python (Python) 2013-06-03T18:08:19-07:00Captain DeadBoneshttp://code.activestate.com/recipes/users/4184772/http://code.activestate.com/recipes/578548-complex-methods-to-compute-pi-in-python/ <p style="color: grey"> Python recipe 578548 by <a href="/recipes/users/4184772/">Captain DeadBones</a> (<a href="/recipes/tags/decimal/">decimal</a>, <a href="/recipes/tags/pi/">pi</a>, <a href="/recipes/tags/python/">python</a>). </p> <p>This script was written for an article I wrote about computing Pi with Python. This module uses the Decimal library. For more information <a href="http://thelivingpearl.com/2013/05/28/computing-pi-with-python/">How to Compute Pi in Python</a></p> Archimedes Method for PI (arbitrary precision) (Python) 2013-03-02T21:56:42-08:00Bjorn Madsenhttp://code.activestate.com/recipes/users/4176352/http://code.activestate.com/recipes/578478-archimedes-method-for-pi-arbitrary-precision/ <p style="color: grey"> Python recipe 578478 by <a href="/recipes/users/4176352/">Bjorn Madsen</a> (<a href="/recipes/tags/decimal/">decimal</a>, <a href="/recipes/tags/math/">math</a>, <a href="/recipes/tags/pi/">pi</a>, <a href="/recipes/tags/precision/">precision</a>, <a href="/recipes/tags/python3/">python3</a>). </p> <p>The code below incorporates the newly revised python 3.3.0 decimal module to permit arbitrary precision (<a href="http://docs.python.org/3/library/decimal.html%29" rel="nofollow">http://docs.python.org/3/library/decimal.html)</a></p> <p>The recipe was originally by FB35 from <a href="http://code.activestate.com/recipes/576981-archimedes-method-for-calculating-pi/" rel="nofollow">http://code.activestate.com/recipes/576981-archimedes-method-for-calculating-pi/</a> </p> A Shell, Binary To Hexadecimal To Decimal Demo... (Bash) 2013-01-11T18:31:03-08:00Barry Walkerhttp://code.activestate.com/recipes/users/4177147/http://code.activestate.com/recipes/578413-a-shell-binary-to-hexadecimal-to-decimal-demo/ <p style="color: grey"> Bash recipe 578413 by <a href="/recipes/users/4177147/">Barry Walker</a> (<a href="/recipes/tags/apple/">apple</a>, <a href="/recipes/tags/bash/">bash</a>, <a href="/recipes/tags/binary/">binary</a>, <a href="/recipes/tags/decimal/">decimal</a>, <a href="/recipes/tags/demo/">demo</a>, <a href="/recipes/tags/hexadecimal/">hexadecimal</a>, <a href="/recipes/tags/linux/">linux</a>, <a href="/recipes/tags/macbook_pro/">macbook_pro</a>, <a href="/recipes/tags/shell/">shell</a>, <a href="/recipes/tags/terminal/">terminal</a>). </p> <p>This little DEMO may be common knowledge to the big guns but not to amateurs like myself, so......</p> <p>This is an Apple Macbbok Shell/Terminal DEMO shell script to show how to:-</p> <p>1) Create a binary file... 2) Save it to your DEFAULT /directory/drwawer/folder/... 3) Display a hexadecimal dump of said binary file to prove that it is binary... 4) Select a single BYTE of that file and save it as an ASCII text decimal _number_, also to your DEFAULT /directory/drawer/folder/... 5) Read this ASCII text decimal number back in again... 6) Add this string representation to a number... 7) Stop...</p> <p>It was intended purely for OSX 10.7.5 and above using the default terminal and shell...</p> <p>It does work on many Linux flavours and shells/terminals also however.</p> <p>Written so the anyone can understand what is going on.</p> <p>The two files generated and saved in this DEMO to your DEFAULT /directory/drawer/folder/ are:-</p> <p>BinaryString.dat BinaryString.txt</p> <p>This WILL lead to something very unusual in the not too distant future...</p> <p>This is Public Domain and you may do with it as you wish...</p> <p>Enjoy finding simple solutions to often very difficult problems...</p> <p>Bazza, G0LCU...</p> Decimal Number To Byte(s) And String To Byte(s) Converter. (Python) 2012-01-24T21:11:47-08:00Barry Walkerhttp://code.activestate.com/recipes/users/4177147/http://code.activestate.com/recipes/578025-decimal-number-to-bytes-and-string-to-bytes-conver/ <p style="color: grey"> Python recipe 578025 by <a href="/recipes/users/4177147/">Barry Walker</a> (<a href="/recipes/tags/bytes/">bytes</a>, <a href="/recipes/tags/converter/">converter</a>, <a href="/recipes/tags/decimal/">decimal</a>, <a href="/recipes/tags/linux/">linux</a>, <a href="/recipes/tags/string/">string</a>, <a href="/recipes/tags/windows/">windows</a>). </p> <p>A function to convert decimal integer numbers, (from 0 to 255), into byte(s) format. Another function calling the above function to convert ASCII strings into byte(s) format.</p> <p>Python 3.1.3 (r313:86834, Nov 28 2010, 10:01:07) [GCC 4.4.5] on linux2 Type "help", "copyright", "credits" or "license" for more information.</p> <pre class="prettyprint"><code>&gt;&gt;&gt; exec(open('/home/G0LCU/Desktop/Code/d2b.py').read()) &gt;&gt;&gt; a=78 &gt;&gt;&gt; type(a) &lt;class 'int'&gt; &gt;&gt;&gt; b=d2b(a) &gt;&gt;&gt; print(b) b'N' &gt;&gt;&gt; type(b) &lt;class 'bytes'&gt; &gt;&gt;&gt; text="\x00(C)2012, B.Walker, G0LCU.\xFF" &gt;&gt;&gt; len(text) 27 &gt;&gt;&gt; type(text) &lt;class 'str'&gt; &gt;&gt;&gt; newtext=t2b(text) &gt;&gt;&gt; len(newtext) 27 &gt;&gt;&gt; print(newtext) b'\x00(C)2012, B.Walker, G0LCU.\xff' &gt;&gt;&gt; type(newtext) &lt;class 'bytes'&gt; </code></pre> <p>It requires NOTHING special at all to work and can be run like above or imported from the correct "Lib" drawer/folder/directorfy as:-</p> <pre class="prettyprint"><code>&gt;&gt;&gt; import d2b </code></pre> <p>And when imported called as:-</p> <pre class="prettyprint"><code>&gt;&gt;&gt; d2b.d2b(some_number, optional_some_other_mumber)&lt;RETURN/ENTER&gt; </code></pre> <p>OR</p> <pre class="prettyprint"><code>&gt;&gt;&gt; d2b.t2b(some_ASCII_string)&lt;RETURN/ENTER&gt; </code></pre> <p>Read the code for much more information...</p> <p>Issued under the GPL2 licence.</p> <p>Enjoy finding simple solutions to often very difficult problems.</p> <p>Bazza, G0LCU.</p> Unit Conversions Using Decimal (Python) 2011-04-13T06:55:05-07:00Dave Baileyhttp://code.activestate.com/recipes/users/4168479/http://code.activestate.com/recipes/577652-unit-conversions-using-decimal/ <p style="color: grey"> Python recipe 577652 by <a href="/recipes/users/4168479/">Dave Bailey</a> (<a href="/recipes/tags/conversions/">conversions</a>, <a href="/recipes/tags/decimal/">decimal</a>, <a href="/recipes/tags/units/">units</a>). </p> <p>A table driven routine generates a unit conversion module and a doctest file. The generated module uses the Decimal class to create unit conversion classes which uses class properties to perform the conversion. conversion.py supplies the following classes: Distance Area Volumn Time Velocity Acceleration Mass Force Weight Pressure Frequency Power Temperature</p> <p>New classes and additional units can easily be created by adding to the table.</p> Decimal and binary prefix Python snippet (Python) 2010-05-06T17:09:44-07:00Romain Dartigueshttp://code.activestate.com/recipes/users/4167472/http://code.activestate.com/recipes/577220-decimal-and-binary-prefix-python-snippet/ <p style="color: grey"> Python recipe 577220 by <a href="/recipes/users/4167472/">Romain Dartigues</a> (<a href="/recipes/tags/binary/">binary</a>, <a href="/recipes/tags/decimal/">decimal</a>, <a href="/recipes/tags/prefix/">prefix</a>). </p> <p>Really short snipped of a decimal and binary prefix calculator in Python.</p> <p>Without covering the entire International System of Units (SI) range, it does fit my daily needs. <br /> The code could be <em>way</em> smarter, but i wanted to keep the recipe as basic as i could.</p> <p>Tested on <code>Python 2.4.1</code> and <code>2.6.4;</code> could work on lower as well.</p> Metric Prefix Symbol and Decimal Quiz (Python) 2009-10-11T18:39:58-07:00Daniel Sahahttp://code.activestate.com/recipes/users/4171644/http://code.activestate.com/recipes/576928-metric-prefix-symbol-and-decimal-quiz/ <p style="color: grey"> Python recipe 576928 by <a href="/recipes/users/4171644/">Daniel Saha</a> (<a href="/recipes/tags/base/">base</a>, <a href="/recipes/tags/decimal/">decimal</a>, <a href="/recipes/tags/metric/">metric</a>, <a href="/recipes/tags/prefix/">prefix</a>, <a href="/recipes/tags/symbol/">symbol</a>, <a href="/recipes/tags/units/">units</a>). Revision 3. </p> <p>Quiz on the metric prefixs, symbols, and decimals associated with each other.</p> Converty decimal/integer number to binary string in Python (Python) 2009-07-17T09:17:32-07:00Vishal Saprehttp://code.activestate.com/recipes/users/4166412/http://code.activestate.com/recipes/576847-converty-decimalinteger-number-to-binary-string-in/ <p style="color: grey"> Python recipe 576847 by <a href="/recipes/users/4166412/">Vishal Sapre</a> (<a href="/recipes/tags/binary/">binary</a>, <a href="/recipes/tags/decimal/">decimal</a>, <a href="/recipes/tags/integer/">integer</a>, <a href="/recipes/tags/string/">string</a>). </p> <p>This is a very simple look-up based method for converting integer to binary string. In my tests, using 'timeit', its the fastest around. And with increasing value of input decimal number, it surpases any 'run time computation' based method doing the above.</p> Base Conversion decimal to base = len(map) (Python) 2008-08-20T05:01:39-07:00Mark Zitnikhttp://code.activestate.com/recipes/users/4166559/http://code.activestate.com/recipes/576435-base-conversion-decimal-to-base-lenmap/ <p style="color: grey"> Python recipe 576435 by <a href="/recipes/users/4166559/">Mark Zitnik</a> (<a href="/recipes/tags/base/">base</a>, <a href="/recipes/tags/conversion/">conversion</a>, <a href="/recipes/tags/decimal/">decimal</a>, <a href="/recipes/tags/sequence/">sequence</a>, <a href="/recipes/tags/web_site/">web_site</a>). Revision 7. </p> <p>This code enable decimal base conversion according map length and a different char set.</p> <p>Example:</p> <ul> <li>map = ['0','1'] base 2 10 -> 1010</li> <li>map = ['a','b'] base 2 10 -> baba</li> <li>map = ['a','b','c','d','e','f','g','h','i','j','k','l'] base 12 10 -> k</li> <li>map = ['a','b','c','d','e','f','g','h','i','j','k','l'] base 12 100 -> ie</li> </ul> <p>this simple method can be used in web sites to hide a well known decimal sequence like user ids.</p>