I am working in an older version of Python (2.6, I believe). I cannot update to the latest version because it is not currently compatible with the other program I need the code to run with so, unfortunately, the "decimal" module is not available to me (as far as I know).
I am running a program that pulls numbers (monetary values) out of a csv file and does some basic math with them before sending the values to the other program being used.
I cannot use int values because both dollars and cents are used, so I have to be able to use two decimal places.
The float functions are difficult to use because the numbers are stored in the memory differently than they appear on screen, so something like 0.1 + 0.2 yields some decimal with too many decimal places for my purposes. I cannot find any information on setting precision to these numbers and since it is a floating point number, I don't really expect to.
I also need to be able to have a function to add commas in the appropriate places for numbers above 999.
Any ideas on the best solution here?
rhuffman8
0
Light Poster
Recommended Answers
Jump to PostYou can multiply the number by 100 and use integers, or truncate to 2 decimal places when printing floats. On most modern computers a float is a binary number as defined by the IEEE Floating Point Arithmetic Standard and represents numbers 1,000,000,000,000 to 0.0000000000000001 (at a minimum). Floats (actually a …
All 4 Replies
woooee
814
Nearly a Posting Maven
rhuffman8
0
Light Poster
griswolf
304
Veteran Poster
Gribouillis
1,391
Programming Explorer
Team Colleague
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.