rounding

Python: Assigning # values in a list to bins, by rounding up

混江龙づ霸主 提交于 2019-12-23 12:51:38
问题 I want a function that can take a series and a set of bins, and basically round up to the nearest bin. For example: my_series = [ 1, 1.5, 2, 2.3, 2.6, 3] def my_function(my_series, bins): ... my_function(my_series, bins=[1,2,3]) > [1,2,2,3,3,3] This seems to be very close to what Numpy's Digitize is intended to do, but it produces the wrong values (asterisks for wrong values): np.digitize(my_series, bins= [1,2,3], right=False) > [1, 1*, 2, 2*, 2*, 3] The reason why it's wrong is clear from

How does Rounding in Python work?

浪子不回头ぞ 提交于 2019-12-23 10:28:11
问题 I am a bit confused about how rounding in Python works. Could someone please explain why Python behaves like this? Example: >>> round(0.05,1) # this makes sense 0.1 >>> round(0.15,1) # this doesn't make sense! Why is the result not 0.2? 0.1 And same for: >>> round(0.25,1) # this makes sense 0.3 >>> round(0.35,1) # in my opinion, should be 0.4 but evaluates to 0.3 0.3 Edit: So in general, there is a possibility that Python rounds down instead of rounding up. So am I to understand that the only

Python Round Function Issues with pyspark

大憨熊 提交于 2019-12-23 09:59:16
问题 I am relatively new to spark and I've run into an issue when I try to use python's builtin round() function after importing pyspark functions. It seems to have to do with how I import the pyspark functions but I am not sure what the difference is or why one way would cause issues and the other wouldn't. Expected behavior: import pyspark.sql.functions print(round(3.14159265359,2)) >>> 3.14 Unexpected behavior: from pyspark.sql.functions import * print(round(3.14159265359,2)) >>> ERROR

Oracle Bankers rule

烂漫一生 提交于 2019-12-23 09:25:38
问题 Why Oracle is not using Bankers rule (the rounding method)? 回答1: Accurate decimal arithmatic is a large and complex subject. Google 'mike colishaw decimal rounding' if you want to read the ahem Oracle on the subject. Basically there are many rounding schemes which are possible:- Round everthing down - the default in most languages including C as Oracle is written in C this is probably why they do this. Round everything up - rarely seen but occasionally needs to be implemented because of

Android: java rounding error. Can't understand why?

≡放荡痞女 提交于 2019-12-23 08:54:28
问题 Can anybody explain why on Earth these "same" expressions returns different values? (new BigDecimal(String.valueOf(131.7d))).multiply(new BigDecimal(String.valueOf(0.95d))).doubleValue() = 125.115 (new BigDecimal( 131.7d )).multiply(new BigDecimal( 0.95d )).doubleValue() = 125.11499999999998 What BigDecimal is doing different between them? 回答1: If you read the API documentation, you will find taht String.valueOf(dobule) uses Double.toString(double) to format the value. It's perhaps not

VBA Round Function

孤者浪人 提交于 2019-12-23 07:46:14
问题 In one of my programs I need to calculate the average on a range of numbers. The problem is that sometimes the answer comes out to a huge decimal and I need to try and get that down to two decimal places maximum. I've seen something called Excel.WorksheetFunction.Round(...) but as far as I know it only works with a specific number or a specific cell. I was wondering if anyone knew how to implement this so that it will round all decimals in a range of cells. Excel.WorksheetFunction.Round

Why can't c# calculate exact values of mathematical functions

我只是一个虾纸丫 提交于 2019-12-23 07:38:12
问题 Why can't c# do any exact operations. Math.Pow(Math.Sqrt(2.0),2) == 2.0000000000000004 I know how doubles work, I know where the rounding error is from, I know that it's almost the correct value, and I know that you can't store infinite numbers in a finite double. But why isn't there a way that c# can calculate it exactly, while my calculator can do it. Edit It's not about my calculator, I was just giving an example: http://www.wolframalpha.com/input/?i=Sqrt%282

How to round a decimal up?

眉间皱痕 提交于 2019-12-23 06:56:48
问题 Given a decimal '96.154', how can I ensure that it is always rounded up to 96.16 (as opposed to normal rounding to 2 decimals which would give 96.15). 回答1: Kind of hacky but a very intuitive way to do so: var val = 96.154M; var result = Math.Ceiling(val * 100) / 100.0M; 回答2: You can add 0.005 to the value and then round the result. 回答3: I think your looking for the Math.Ceiling method. You could combine this with a multiplier to specify how many decimal places to round. Like this, public

Round datetime to last hour

我的梦境 提交于 2019-12-23 06:50:13
问题 I tried to look for this but I could not find good example of this what im trying to do. I got datetime values in MySQL database that has to be rounded down when that value is on use. Example, all these values: 2013-04-20 07:14:42 2013-04-20 07:19:51 2013-04-20 07:37:26 2013-04-20 07:46:28 2013-04-20 07:59:44 Should be rounded down to: 2013-04-20 07:00:00 And 2013-04-20 16:25:34 should be: 2013-04-20 16:00:00 etc... PHP code that gets date value: $d = strtotime($row["date"]); So, how its

Round Double to 1 decimal place kotlin: from 0.044999 to 0.1

◇◆丶佛笑我妖孽 提交于 2019-12-23 06:48:13
问题 I have a Double variable that is 0.0449999 and I would like to round it to 1 decimal place 0.1 . I am using Kotlin but the Java solution is also helpful. val number:Double = 0.0449999 I tried getting 1 decimal place with these two solutions: val solution = Math.round(number * 10.0) / 10.0 val solution = String.format("%.1f", number) The problem is that I get 0.0 in both cases because it rounds the number from 0.04 to 0.0 . It doesn't take all decimals and round it. I would like to obtain 0.1: