decimal

How to use Python to convert an octal to a decimal

廉价感情. 提交于 2019-12-05 04:57:34
I have this little homework assignment and I needed to convert decimal to octal and then octal to decimal. I did the first part and can not figure out the second to save my life. The first part went like this: decimal = int(input("Enter a decimal integer greater than 0: ")) print("Quotient Remainder Octal") bstring = " " while decimal > 0: remainder = decimal % 8 decimal = decimal // 8 bstring = str(remainder) + bstring print ("%5d%8d%12s" % (decimal, remainder, bstring)) print("The octal representation is", bstring) I read how to convert it here: Octal to Decimal but have no clue how to turn

Modulo operation on a python negative decimal.Decimal and a positive int

夙愿已清 提交于 2019-12-05 04:56:59
With simple int s: >>> -45 % 360 315 Whereas, using a decimal.Decimal : >>> from decimal import Decimal >>> Decimal('-45') % 360 Decimal('-45') I would expect to get Decimal('315') . Is there any reason for this? Is there a way to get a consistent behaviour (without patching decimal.Decimal )? (I did not change the context, and cannot find how it could be changed to solve this situation). Python behaves according to IBM's General Decimal Arithmetic Specification . The remainder is defined as: remainder takes two operands; it returns the remainder from integer division. […] the result is the

Always display at least two decimal places

流过昼夜 提交于 2019-12-05 03:02:13
I want to format a number so that it always have at least two decimal places. Samples: 1 2.1 123.456 234.45 Output: 1.00 2.10 123.456 234.45 You could fix to 2 or the count of current places; var result = num.toFixed(Math.max(2, (num.toString().split('.')[1] || []).length)); Try this: var num = 1.2; function decimalPlaces(num) { var match = (''+num).match(/(?:\.(\d+))?(?:[eE]([+-]?\d+))?$/); if (!match) { return 0; } return Math.max( 0, // Number of digits right of decimal point. (match[1] ? match[1].length : 0) // Adjust for scientific notation. - (match[2] ? +match[2] : 0)); } if

Determine the decimal precision of an input number

柔情痞子 提交于 2019-12-05 02:44:21
We have an interesting problem were we need to determine the decimal precision of a users input (textbox). Essentially we need to know the number of decimal places entered and then return a precision number, this is best illustrated with examples: 4500 entered will yield a result 1 4500.1 entered will yield a result 0.1 4500.00 entered will yield a result 0.01 4500.450 entered will yield a result 0.001 We are thinking to work with the string, finding the decimal separator and then calculating the result. Just wondering if there is an easier solution to this. Daniel Brückner I think you should

Limit Numbers after Decimal on Key Press Event

流过昼夜 提交于 2019-12-05 02:17:56
问题 I am using the following code to take only digits from user and only one decimal point , that is working fine for me on KeyPress Event : if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && e.KeyChar != '.') { e.Handled = true; } if (e.KeyChar == '.' && (sender as TextBox).Text.IndexOf('.') > -1) { e.Handled = true; } Now I want to Limit the numbers/Digits after the decimal/dot i.e 35.25468, means it take only 6 numbers/digits after the dot/decimal. Update me ! 回答1: private void

Python 3 Decimal rounding half down with ROUND_HALF_UP context

十年热恋 提交于 2019-12-05 02:17:22
Can anybody explain or propose a fix for why when I round a decimal in Python 3 with the context set to round half up, it rounds 2.5 to 2, whereas in Python 2 it rounds correctly to 3: Python 3.4.3 and 3.5.2: >>> import decimal >>> context = decimal.getcontext() >>> context.rounding = decimal.ROUND_HALF_UP >>> round(decimal.Decimal('2.5')) 2 >>> decimal.Decimal('2.5').__round__() 2 >>> decimal.Decimal('2.5').quantize(decimal.Decimal('1'), rounding=decimal.ROUND_HALF_UP) Decimal('3') Python 2.7.6: >>> import decimal >>> context = decimal.getcontext() >>> context.rounding = decimal.ROUND_HALF_UP

Specify number of decimals when serializing currencies with JSONSerialization

和自甴很熟 提交于 2019-12-05 02:16:47
问题 NumberFormatter makes it quite easy to format currencies when presenting values on screen: let decimal = Decimal(25.99) let decimalNumberFormatter = NumberFormatter() decimalNumberFormatter.numberStyle = .currencyAccounting let output = decimalNumberFormatter.string(for: decimal) // output = "$25.99" The above code works well both for any Decimal or Double values. The amount of decimal digits always matches that of the locale being used. Turns our that serializing a floating point currency

How do I round down a decimal to 2 decimal places in .Net?

女生的网名这么多〃 提交于 2019-12-05 02:06:53
This should be easy, but I can’t find a built in method for it, the .net framework must have a method to do this! private decimal RoundDownTo2DecimalPlaces(decimal input) { if (input < 0) { throw new Exception("not tested with negitive numbers"); } // There must be a better way! return Math.Truncate(input * 100) / 100; } If you are rounding down then you need: Math.Floor(number * 100) / 100; if you are looking for something called 'bankers rounding' (probably not if it's for output and not for statistics/summing) then: Math.Round(number, 2); Finally if you want, not sure what the correct term

Overflow exception when reading decimal values from SQL Server

会有一股神秘感。 提交于 2019-12-05 02:06:46
I'm wondering whether this is a bug or if I'm going something wrong. I'm loading values with a SqlDataReader from a SQL Server 2008 database but under certain circumstances, it fails to convert the SQL values into .net values. (.NET 4.0) I have traced it down to an test-case which demonstrates the actual problem: Working example: "select convert(decimal(38, 19), 260000 ) as test" rs.GetValue(1); --> returns 260000 (decimal) Not working exmaple: "select convert(decimal(36, 26), 260000 ) as test" rs.GetValue(1); --> throws System.OverflowException: Conversion overflows. at System.Data.SqlClient

MYSQL: DECIMAL with accuracy of 10 digits after the comma

前提是你 提交于 2019-12-05 02:01:52
In MySQL I have a DECIMAL field set to 10,10. However, whenever I enter a new value, it shows 0.9999999999. What would I need to change to accept any number with an accuracy of 10 digits after the comma? It does work with 10,6 for some reason. PS: I want to insert exchange rates. The first number is the total number of digits to store, the second one is the number of digits on the fractional part. So DECIMAL (10, 10) is only for 10 fractional digits. You can use something like DECIMAL (20, 10) to allow 10 digits on both the integral and fractional parts. DECIMAL(10,10) means there's no decimal