decimal

round decimal numbers in Python

round decimal numbers in Python Question: How can I render decimal numbers in Python? For example, in the number 3.14373873 only up to and including the number 4. I want to round the decimal numbers from a specific place: As example: 3.14373873 -> 3.14 Asked By: Tan || Source Answers: you can use the round() …

Total answers: 4

Maximizing Multiplier Based on Precision in Python

Maximizing Multiplier Based on Precision in Python Question: Given the following equation: x / y = z Where the precision of x is 5 and the precision of z is 8, given the following values of x, y: from decimal import Decimal x = Decimal(‘15.00000’) y = Decimal(‘81.63’) x_prec = 5 z_prec = 8 z_unrounded …

Total answers: 2

Python Decimal module stops adding Decimals to another Decimal once it reaches 1.0

Python Decimal module stops adding Decimals to another Decimal once it reaches 1.0 Question: I am using python’s decimal module to do some work involving decimals. I have the following code: from decimal import * getcontext().prec = 2 # use two decimal places counter = Decimal(0) while counter != Decimal(1000.01): print(counter) counter += Decimal(0.01) This …

Total answers: 1

How to make that f"…" string formatting uses comma instead of dot as decimal separator?

How to make that f"…" string formatting uses comma instead of dot as decimal separator? Question: I tried: import locale print(locale.locale_alias) locale.setlocale(locale.LC_ALL, ”) locale.setlocale(locale.LC_NUMERIC, "french") print(f"{3.14:.2f}") but the output is 3.14 whereas I would like 3,14. How to do this with f"…" string formatting? Note: I don’t want to use .replace(".", ",") Note: I’m looking …

Total answers: 3

How to convert incredibly long decimals to fractions and back with high precision

How to convert incredibly long decimals to fractions and back with high precision Question: I’m trying to convert very large integers to decimals, then convert those decimals to Fractions, and then convert the Fraction back to a decimal. I’m using the fractions and decimal packages to try and avoid floating point imprecision, however the accuracy …

Total answers: 1

Extract query returning Decimal('value'), float is expected

Extract query returning Decimal('value'), float is expected Question: I saw a topic with the same question on Stackoverflow but I have a bit different question. My system locally return the value as float, but on GitHub Actions its Decimal. What could be the reason session = app.ReadOnlySession() query_ = text(query) result_proxy = session.execute(query_, fetched_options) res …

Total answers: 1

Removing decimals from strings

Removing decimals from strings Question: I’m having an introductory course in python right now and i get into some troubles with the task. I have two strings in format: a b c d e f g h i l I need to get this strings from .txt file,convert them as matrix to vertical format like …

Total answers: 2

Dividing a 24-digit binary number to 3 equal parts

Dividing a 24-digit binary number to 3 equal parts Question: I’d like to know how can I divide a 24-digit binary number which is taken from user in python to 3 parts and then put different conditions on each of these 3 parts. e.g: input => 111100011110110100100100 divide the input to 3 equal parts (each …

Total answers: 1

Creating python decimal with locale formatted string

Creating python decimal with locale formatted string Question: I am using the Python decimal module, I’ve set my locale to Germany such that locale.localeconv()["decimal_point"] is ",". When I try creating a decimal like decimal.Decimal("1,23"), I get a decimal conversion syntax error. When I do decimal.Decimal("1.23"), it works. Does the Python decimal not respect locale settings …

Total answers: 1

Using decimals in math functions in python

Using decimals in math functions in Python Question: math is a Python module used by many to do a bit more advanced mathematical functions and using the decimal module. One can calculate stuff correctly 1.2-1.1=0.0999~, but using the decimal type it’s 0.1. My problem is that these two modules don’t work well with each other. …

Total answers: 2