decimal

Why does float() cut off trailing zeros?

微笑、不失礼 提交于 2019-12-11 03:57:02
问题 The code successfully crops a large file of many numbers to several smaller text files with number, but it produces an interesting quirk. All numbers should be to four decimal points like 2.7400 but instead they print as 2.74. Here is a snippet from a file 0.96 0.53 0.70 0.53 0.88 0.97 Why does this happen? Is there a way to fix this or this just a quirk of float()? from itertools import islice def number_difference(iterable): return float(iterable[-1].strip('\n')) - float(iterable[0].strip('

AngularJS {{ val | number:1 }} not rounding to 1 decimal place

徘徊边缘 提交于 2019-12-11 03:54:58
问题 <p> <b>{{ x.title }} </b> Rating:{{ x.rating | number:1}}</p> The above code is what I'm using. It is getting the value correctly because it is showing an unrounded number (e.g. 3.333333333) so I know the value is passed correctly. Why won't the number:# filter work? 回答1: I'm an idiot. CLEAR YOUR CACHE KIDS - It was working but hadn't reloaded my index.html file. 来源: https://stackoverflow.com/questions/29421770/angularjs-val-number1-not-rounding-to-1-decimal-place

C libpq: get float value from numeric

半腔热情 提交于 2019-12-11 03:43:02
问题 I have a table with float value represented using numeric type. How can I get float value from this field using libpq lib? There is a specialized format for float type in postgres, but I have to work with existing database with numeric fields. I haven't found information about it in the Postgres documentation so every info is appreciated. 回答1: Strictly you can't get an accurate float representation of a numeric . Not for any arbitrary numeric anyway. numeric is arbitrary-precision decimal

Python - decimal to integer low byte then high byte

余生颓废 提交于 2019-12-11 03:29:52
问题 Hello I'm a newbie with Python but I've been interested on it for 2 years. I want to make robots and I'm trying to use Python with pyserial on blender. But I found a problem and after 2 hours of looking for the answer in Google and in this site I found that maybe I'm retarded because I can't solve it. I think it isn't asked yet. I'm using a devantech sd84 servo controller and controling it via a USB port a serial device so I use pyserial . The problem is that I want Python to take a decimal

Decimal to Binary in C

怎甘沉沦 提交于 2019-12-11 02:59:41
问题 I'm creating a program that adds and subtracts 2 numbers. Then I have to output this answer into different bases. My answer is in decimal format, of type long double, such as: long double answer; answer = numberOne + numberTwo; I want to convert this answer into binary. Now I have code used earlier in my program that does this, but with a char pointer: char * decimalBinary (char * decimalNumber) { bool zeroFront = true; int i; int z; int j = 0; int n = atoi(decimalNumber); char * binaryNum =

Json.NET decimal precision loss

﹥>﹥吖頭↗ 提交于 2019-12-11 02:34:17
问题 I have an issue with deserializing decimal value. JObject.Parse("{\"available\":8777.831438322572000}") If I type this code in VS under debugger the result is "available": 8777.8314383225716 If I try this obj.Value<decimal>("available") the result is 8777.83143832257 Where am I wrong? What api methods should I use to get correct results? 回答1: The result of JObject.Parse("{\"available\":8777.831438322572000}") is a double . The second statement results in a decimal . The double has floating

How to split up a two-digit number in C

随声附和 提交于 2019-12-11 02:29:09
问题 Let's say I have a number like 21 and I want to split it up so that I get the numbers 2 and 1. To get 1, I could do 1 mod 10. So basically, the last digit can be found out by using mod 10. To get 2, I could do (21 - (1 mod 10))/10. The above techniques will work with any 2-digit number. However, let me add a further constraint, that mod can only be used with powers of 2. Then the above method can't be used. What can be done then? 回答1: 2 == 23 / 10 3 == 23 - (23 / 10) * 10 回答2: To get 2 you

Conversion of decimal floating point numbers to binary and back

守給你的承諾、 提交于 2019-12-11 02:26:26
问题 My question, in short is, why does rounding error in floats only show up after calculations and not for storage of literals? What I mean is this - I know about the issues that arise due to rounding error in floats when converting from decimal to binary and back. Eg, in Java: double a = 10.567; double b = 2.16; double c = a * b; c then stores the value 22.824720000000003, instead of 22.82472. This is because the result 22.82472 cannot be stored accurately in the finite binary digits of the

Rounding a Decimal number in Elixir

雨燕双飞 提交于 2019-12-11 01:38:28
问题 I have this Decimal number Elixir: c1 = Decimal.div(a1, b1) # => #Decimal<0.006453859622556080696687234694> How can I round it to a shorter number with a smaller number of digits after the decimal point? 回答1: As @Dogbert said in comment - the easiest way to just round a number: iex> alias Decimal, as: D nil iex> d = D.div(D.new(1), D.new(3)) #Decimal<0.3333333333333333333333333333> iex> D.round(d, 5) #Decimal<0.33333> EDIT: Below is not entirely true!! Precision is number of digits in

jQuery round decimal to .49 or .99

我只是一个虾纸丫 提交于 2019-12-11 01:37:08
问题 I'm trying to work out how to round a decimal to .49 or .99. I have found the toFixed(2) function, but not sure how to round up or down. Basically need to get to the closest price point, so for example X.55 would go down to X.49 and X.84 would go up to X.99. 回答1: This doesn’t require jQuery but can be done with plain JavaScript: Math.round(price*2)/2 - 0.01 Note to also consider the case where the number would get rounded to 0 ( price > 0.25) as that would yield -0.01 in this case. 回答2: If I