decimal

Convert a string to integer with decimal in Python

爱⌒轻易说出口 提交于 2019-12-17 08:56:28
问题 I have a string in the format: 'nn.nnnnn' in Python, and I'd like to convert it to an integer. Direct conversion fails: >>> s = '23.45678' >>> i = int(s) Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: invalid literal for int() with base 10: '23.45678' I can convert it to a decimal by using: >>> from decimal import * >>> d = Decimal(s) >>> print d 23.45678 I could also split on '.', then subtract the decimal from zero, then add that to the whole number ...

Calculate System.Decimal Precision and Scale

拟墨画扇 提交于 2019-12-17 08:31:34
问题 Suppose that we have a System.Decimal number. For illustration, let's take one whose ToString() representation is as follows: d.ToString() = "123.4500" The following can be said about this Decimal. For our purposes here, scale is defined as the number of digits to the right of the decimal point. Effective scale is similar but ignores any trailing zeros that occur in the fractional part. (In other words, these parameters are defined like SQL decimals plus some additional parameters to account

Convert exponential number to decimal in php

╄→尐↘猪︶ㄣ 提交于 2019-12-17 07:54:14
问题 I have a floating point number in exponential format i.e. 4.1595246940817E-17 and I want to convert it into decimal number like 2.99 etc. Any help will be appreciated. format_number() sprintf() don't seem to be working for me. 回答1: You need a better math extension like BC Math, GMP... to handle the more precise precision. Limitation of floating number & integer 回答2: Using the BC Math library you can bcscale() the numbers to a predetermined decimal, which sets the parameter for future

Convert exponential number to decimal in php

感情迁移 提交于 2019-12-17 07:54:11
问题 I have a floating point number in exponential format i.e. 4.1595246940817E-17 and I want to convert it into decimal number like 2.99 etc. Any help will be appreciated. format_number() sprintf() don't seem to be working for me. 回答1: You need a better math extension like BC Math, GMP... to handle the more precise precision. Limitation of floating number & integer 回答2: Using the BC Math library you can bcscale() the numbers to a predetermined decimal, which sets the parameter for future

drop trailing zeros from decimal

二次信任 提交于 2019-12-17 07:25:08
问题 I have a long list of Decimals and that I have to adjust by factors of 10, 100, 1000,..... 1000000 depending on certain conditions. When I multiply them there is sometimes a useless trailing zero (though not always) that I want to get rid of. For example... from decimal import Decimal # outputs 25.0, PROBLEM! I would like it to output 25 print Decimal('2.5') * 10 # outputs 2567.8000, PROBLEM! I would like it to output 2567.8 print Decimal('2.5678') * 1000 Is there a function that tells the

Adding binary numbers

别来无恙 提交于 2019-12-17 06:05:34
问题 Does anyone know how to add 2 binary numbers, entered as binary, in Java? For example, 1010 + 10 = 1100 . 回答1: Use Integer.parseInt(String, int radix). public static String addBinary(){ // The two input Strings, containing the binary representation of the two values: String input0 = "1010"; String input1 = "10"; // Use as radix 2 because it's binary int number0 = Integer.parseInt(input0, 2); int number1 = Integer.parseInt(input1, 2); int sum = number0 + number1; return Integer.toBinaryString

OverflowError: (34, 'Result too large')

丶灬走出姿态 提交于 2019-12-17 05:04:55
问题 I'am getting an overflow error(OverflowError: (34, 'Result too large') I want to calculate pi to 100 decimals here's my code: def pi(): pi = 0 for k in range(350): pi += (4./(8.*k+1.) - 2./(8.*k+4.) - 1./(8.*k+5.) - 1./(8.*k+6.)) / 16.**k return pi print(pi()) 回答1: Python floats are neither arbitary precision nor of unlimited size. When k = 349, 16.**k is much too large - that's almost 2^1400. Fortunately, the decimal library allows arbitrary precision and can handle the size: import decimal

How to get numbers after decimal point?

。_饼干妹妹 提交于 2019-12-17 04:24:46
问题 How do I get the numbers after a decimal point? For example, if I have 5.55 , how do i get .55 ? 回答1: An easy approach for you: number_dec = str(number-int(number))[1:] 回答2: 5.55 % 1 Keep in mind this won't help you with floating point rounding problems. I.e., you may get: 0.550000000001 Or otherwise a little off the 0.55 you are expecting. 回答3: Use modf: >>> import math >>> frac, whole = math.modf(2.5) >>> frac 0.5 >>> whole 2.0 回答4: What about: a = 1.3927278749291 b = a - int(a) b >> 0

How to get numbers after decimal point?

独自空忆成欢 提交于 2019-12-17 04:24:08
问题 How do I get the numbers after a decimal point? For example, if I have 5.55 , how do i get .55 ? 回答1: An easy approach for you: number_dec = str(number-int(number))[1:] 回答2: 5.55 % 1 Keep in mind this won't help you with floating point rounding problems. I.e., you may get: 0.550000000001 Or otherwise a little off the 0.55 you are expecting. 回答3: Use modf: >>> import math >>> frac, whole = math.modf(2.5) >>> frac 0.5 >>> whole 2.0 回答4: What about: a = 1.3927278749291 b = a - int(a) b >> 0

Formatting a float to 2 decimal places

故事扮演 提交于 2019-12-17 04:15:56
问题 I am currently building a sales module for a clients website. So far I have got the sale price to calculate perfectly but where I have come stuck is formatting the output to 2 decimal places. I am currently calling this in a variable so that I can data bind the results to a listview. Sale = float.Parse(((x.Sale_Price - (x.Sale_Price * (x.Discount_Price / 100))).ToString())), Can anyone show me how to format the output to 2 decimal places?? Many Thanks! 回答1: You can pass the format in to the