Banker's rounding | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
+ 1

Banker's rounding

Please tell me, are there any ways of classic rounding in Python 3?

1st Jan 2020, 10:22 PM
AlisašŸŒøšŸŒø
AlisašŸŒøšŸŒø - avatar
10 Respostas
+ 1
Oh sorry, Aymane Boukrouh. My mistake. AlisašŸŒøšŸŒø, there are no built-in functions to do what you're talking about, but you can create a custom function: import math def bankers_rounding(num): if math.floor(num) % 2 == 0: #if rounding down num returns an even number return math.floor(num) else: return math.ceil(num)
1st Jan 2020, 10:46 PM
Jianmin Chen
Jianmin Chen - avatar
+ 2
Thank you, it was so easy
1st Jan 2020, 10:50 PM
AlisašŸŒøšŸŒø
AlisašŸŒøšŸŒø - avatar
+ 2
Abhishek Sahoo, This is really useful and may come in handy, thank you :)
3rd Jan 2020, 4:28 PM
AlisašŸŒøšŸŒø
AlisašŸŒøšŸŒø - avatar
+ 1
You're welcome.
1st Jan 2020, 10:50 PM
Jianmin Chen
Jianmin Chen - avatar
+ 1
Jianmin Chen 你儽ļ¼Œcan you please test your solution against test cases of 4.6 and 5.3?
2nd Jan 2020, 5:40 AM
Gordon
Gordon - avatar
3rd Jan 2020, 1:50 PM
ABHISHEK
ABHISHEK - avatar
+ 1
Gordon , I'm pretty sure the program works correctly for those two numbers. 4.6 will return 4, and 5.3 will return 6. By the way, 你儽吗ļ¼Ÿęˆ‘会čÆ“äø­ę–‡å’Œč‹±ę–‡ć€‚šŸ˜ƒ
3rd Jan 2020, 7:27 PM
Jianmin Chen
Jianmin Chen - avatar
+ 1
Jianmin Chen ęˆ‘ä¹Ÿęœƒäø­ę–‡å‘€ banker's rounding ēš„話ļ¼Œ4.6還ę˜Æę‡‰č©²é€²ä½ē‚ŗ5ļ¼Œ5.3還ę˜Æę‡‰č©²ęØę£„ē‚ŗ5怂åŖ꜉.5ę™‚ę‰č¦ę‰¾ęœ€ęŽ„čæ‘é›™ę•øļ¼Œä¾†å¹³č””å¤šę¬”åŠ ęø›ēš„čŖ¤å·®bias怂 見ļ¼š https://en.m.wikipedia.org/wiki/Rounding 引ē”Øäø€ļ¼š Round half to even Edit A tie-breaking rule without positive/negative bias and without bias toward/away from zero is round half to even. By this convention, if the fractional part of x is 0.5, then y is the even integer nearest to x. Thus, for example, +23.5 becomes +24, as does +24.5; while āˆ’23.5 becomes āˆ’24, as does āˆ’24.5. This function minimizes the expected error when summing over rounded figures, even when the inputs are mostly positive or mostly negative. This variant of the round-to-nearest method is also called convergent rounding, statistician's rounding, Dutch rounding, Gaussian rounding, oddā€“even rounding,[6] or bankers' rounding. č§£ę±ŗēš„問锌ļ¼Œč¦‹ Vancouver Stock Exchangeé‚£ę®µć€‚
4th Jan 2020, 2:28 AM
Gordon
Gordon - avatar
+ 1
Jianmin Chen yes it is correct banker's rounding now~ šŸ‘ ē”­å®¢ę°”ļ½ž
4th Jan 2020, 3:10 AM
Gordon
Gordon - avatar
0
Gordon, I see my error! Thanks for informing me, I wouldn't have known. So, AlisašŸŒøšŸŒø, this means the correct code is: import math def bankers_rounding(num): if (num - 0.5).is_integer() and (num + 0.5).is_integer(): #if number is exactly between two whole numbers if math.floor(num) % 2 == 0: #if rounding down returns an even number return math.floor(num) else: return math.ceil(num) else: return round(num) #if not exactly between two whole numbers, return the normally rounded amount I believe that's my mistake, right? The number had to be exactly between two whole numbers in order to be rounded in the bankers' rounding method. č°¢č°¢, Gordon.
4th Jan 2020, 2:56 AM
Jianmin Chen
Jianmin Chen - avatar