rounding

Round values of a python dataframe column according to authorized values

我们两清 提交于 2019-12-24 07:22:32
问题 I have this dataframe : df = pd.DataFrame({'id':[1,2,3,4], 'score':[0.35,3.4,5.5,8]}) df id score 0 1 0.35 1 2 3.4 2 3 5.5 3 4 8 and this list : L = list(range(1,7)) L [1, 2, 3, 4, 5, 6] I would like to round the values of df.scores to the closest value in L. Consequently, I would like to get : df id score 0 1 1 1 2 3 2 3 6 3 4 6 I tried something like df['score'].apply(lambda num : min([list(range(1,7)), key = lambda x:abs(x-num)]) but it didn't work (I'm a very beginner, sorry if this

Round values of a python dataframe column according to authorized values

无人久伴 提交于 2019-12-24 07:20:08
问题 I have this dataframe : df = pd.DataFrame({'id':[1,2,3,4], 'score':[0.35,3.4,5.5,8]}) df id score 0 1 0.35 1 2 3.4 2 3 5.5 3 4 8 and this list : L = list(range(1,7)) L [1, 2, 3, 4, 5, 6] I would like to round the values of df.scores to the closest value in L. Consequently, I would like to get : df id score 0 1 1 1 2 3 2 3 6 3 4 6 I tried something like df['score'].apply(lambda num : min([list(range(1,7)), key = lambda x:abs(x-num)]) but it didn't work (I'm a very beginner, sorry if this

Rounding number to two decimal places

社会主义新天地 提交于 2019-12-24 06:45:00
问题 Hy guys, I have a case where a want to round the number to exactly two decimal places. I will give an example and what I tried. Lets say I have 15.07567 To round it up I did: price = Math.round(15.07567 * 100) / 100; // and I get 15.08 But this presents a problems if we have digits that end wit 0 (example 15.10) and we want two decimals. price = Math.round(15.10 * 100) / 100; //15.1 Hmmm, so I tried to use toFixed() price = Math.round(15.10 * 100) / 100; total = price.toFixed(2); // I get "15

Do mobile webkit browsers have a rounding issue in JS?

给你一囗甜甜゛ 提交于 2019-12-24 04:51:44
问题 I'm trying to debug an issue with some javascript slider code on mobile browsers. It appears to be a rounding error which only occurs on mobile devices. The following code will work fine on the desktop (e.g. Chrome) but the increase button fails to work on higher values on the slider when viewed in Webkit on a smartphone e.g. iPhone iOS 5/6, Samsung S2 ICS. Try this http://jsfiddle.net/codecowboy/mLpfu/. Click the 'debug on mobile' button - its directly adjacent to the Run button top left

How to do custom rounding of numbers in Java?

纵然是瞬间 提交于 2019-12-24 03:53:00
问题 Suppose I want to round numbers that have mantissa greater than 0.3 'up' and those below 'down'. How can I do it in Java? The only thing that came to my mind was Math.round() , but I can't seem to make it follow a certain rule. 回答1: Math.floor(x+0.7) should do it. This should work for an arbitrary mantissa. Just add the offset to the next integer to your value and round down. The rounding is done by floor. Here is what the java API says to floor: Returns the largest (closest to positive

Determine if rounding occurred for a floating-point operation in C/C++

百般思念 提交于 2019-12-24 03:43:29
问题 I am trying to come up with an efficient method to determine when rounding will/did occur for IEEE-754 operations. Unfortunately I am not able to simply check hardware flags. It would have to run on a few different platforms. One of the approaches I thought of was to perform the operation in different rounding modes to compare the results. Example for addition: double result = operand1 + operand2; // save rounding mode int savedMode = fegetround(); fesetround(FE_UPWARD); double upResult =

Determine if rounding occurred for a floating-point operation in C/C++

左心房为你撑大大i 提交于 2019-12-24 03:43:12
问题 I am trying to come up with an efficient method to determine when rounding will/did occur for IEEE-754 operations. Unfortunately I am not able to simply check hardware flags. It would have to run on a few different platforms. One of the approaches I thought of was to perform the operation in different rounding modes to compare the results. Example for addition: double result = operand1 + operand2; // save rounding mode int savedMode = fegetround(); fesetround(FE_UPWARD); double upResult =

Swift 3 Migration - Double Extension rounding issue

不打扰是莪最后的温柔 提交于 2019-12-23 22:12:09
问题 I'm migrating our codebase to Swift 3 and I've come across a compilation issue that I can't explain or fix. I have a method in a Double extension that rounds the Double to a certain number of digits: public func roundToPlaces(places: Int) -> Double { let divisor = pow(10.0, Double(places)) return round(self * divisor) / divisor } For example: 12.34567.roundToPlaces(2) should return 12.35 . However, I'm getting a compilation issue for the round method used in this extension. It's saying that I

how to do round half even in javascript unlimited digits after point?

怎甘沉沦 提交于 2019-12-23 21:30:07
问题 I want to have 26.955 rounded to 26.96 I mean if I have numbers 0-4 I want to round them down and if they are 5-9 I want to round them up I considered parseFloat(number).toFixed(2) however it returns me 26.95, I need 26.96 I used Math.round but this doesn't work either, I saw Math.round10 but it told me this function doesn't exist so I don't know how to solve my problem. UPDATE: I don't have always 3 digits after point I have more than that I would have 26.956736489 your mentioned duplicate

Round prices to nearest $xx.99

☆樱花仙子☆ 提交于 2019-12-23 15:43:29
问题 I want to round (up or down) prices to nearest $xx.99 For example: $17.99 -> Stay as is $22.03 -> $21.99 $33.85 -> $33.99 $45 -> $44.99 回答1: Add 0.01, round, minus 0.01 $input = 17.99; // example input $add = $input + 0.01; $round = round($add); $output = $round - 0.01; Or all-in-one: return round($input + 0.01) - 0.01; 来源: https://stackoverflow.com/questions/35129514/round-prices-to-nearest-xx-99