rounding

Python rounding issue

别说谁变了你拦得住时间么 提交于 2019-12-22 03:45:58
问题 I have come across a very strange issue in python. (Using python 2.4.x) In windows: >>> a = 2292.5 >>> print '%.0f' % a 2293 But in Solaris: >>> a = 2292.5 >>> print '%.0f' % a 2292 But this is the same in both windows and solaris: >>> a = 1.5 >>> print '%.0f' % a 2 Can someone explain this behavior? I'm guessing it's platform dependent on the way that python was compiled? 回答1: The function ultimately in charge of performing that formatting is PyOS_snprintf (see the sources). As you surmise,

The right way to round pandas.DataFrame?

六眼飞鱼酱① 提交于 2019-12-22 03:37:28
问题 I want round pandas.DataFrame . Here is what i have tried so far: import pandas as pd data = pd.DataFrame([1.4,2.5,3.8,4.4,5.6],[6.2,7.6,8.8,9.1,0]) print(round(data)) But when i run this code, i get the following error: Traceback (most recent call last): File "C:\Users\*****\Documents\*****\******\****.py", line 3, in <module> print(round(data)) TypeError: type DataFrame doesn't define __round__ method What is the right way to round pandas.DataFrame ? 回答1: first, you may want to change the

The right way to round pandas.DataFrame?

我是研究僧i 提交于 2019-12-22 03:37:08
问题 I want round pandas.DataFrame . Here is what i have tried so far: import pandas as pd data = pd.DataFrame([1.4,2.5,3.8,4.4,5.6],[6.2,7.6,8.8,9.1,0]) print(round(data)) But when i run this code, i get the following error: Traceback (most recent call last): File "C:\Users\*****\Documents\*****\******\****.py", line 3, in <module> print(round(data)) TypeError: type DataFrame doesn't define __round__ method What is the right way to round pandas.DataFrame ? 回答1: first, you may want to change the

Math.round() wrong calculation in as3?

不问归期 提交于 2019-12-22 00:30:17
问题 Can anyone explain this? what am I doing wrong? 回答1: Math.Round(x:Number) rounds x to the nearest integer value. In your case 28 is the nearest integer value for 28.499999999999996. So here the behavior is correct. What is weird is that 0.285 * 100 is not 28.5, but that is a consequence of the precision of the Number class in as3. Here is a little more information about this and a possible solution: Innacurate math results Also you can see this SO question: Very strange number operation issue

How to customize the form of rounding

只谈情不闲聊 提交于 2019-12-21 20:55:53
问题 My question may seem simple, but still can not get something that works. I need to customize the Math.round rounding format or something to make it work as follows: If the number is 1.6 he should round to 1, if greater than or equal to 1.7 should round to 2.0 . And so to all other decimal results with # .6 The way I'm doing the 1.6 being rounded to 2 shall be rounded to 1. How can I do that? Thank you! 回答1: Simply do this: double threshold = 0.7; Math.round(x - threshold + 0.5); 回答2: You

Drawing Images on Html5 Canvas at floating point coordinates

喜夏-厌秋 提交于 2019-12-21 20:47:43
问题 As you know using rounded coordinates while drawing on canvas is faster. Also floating coordinates may cause unintended gaps on canvas. For example, you are drawing google map tiles to canvas, 256x256 tiles work well, if the starting coordinates are integer. If not, to avoid one pixel unpainted lines, you should round the coordinates. Here, that's Ok. But, what if you should use scaling, transformation over canvas, how can you round the coordinates? e.g. ctx.drawImage(image, round(x), round(y

What is the most efficient way to round a float value to the nearest integer in java?

心不动则不痛 提交于 2019-12-21 20:46:36
问题 I've seen a lot of discussion on SO related to rounding float values, but no solid Q&A considering the efficiency aspect. So here it is: What is the most efficient (but correct) way to round a float value to the nearest integer? (int) (mFloat + 0.5); or Math.round(mFloat); or FloatMath.floor(mFloat + 0.5); or something else? Preferably I would like to use something available in standard java libraries, not some external library that I have to import. 回答1: public class Main { public static

Round a number to the next HIGHEST 10

十年热恋 提交于 2019-12-21 10:47:21
问题 I have a need to create a graph, where the scale of the Y-axis changes depending on the data input into the system. Conceivably this scale could be anywhere from 0-10, 0-100, or even have bottom limit of thousands and an upper limit of millions. To properly determinethe scale of this axis, I need to work out the ratio of Points to Pixels (based on graph height/range). Now a graphs' axis never start at the lowest value and go to the highest, usual practice is to go to the next nearest 2, 5 or

C# banker's rounding error

风流意气都作罢 提交于 2019-12-21 09:27:59
问题 double a = 18.565 return Math.Round(a,2) ..returns 18.57. For every other number I tried banker's rounding worked as expected, for example Math.Round(2.565,2) returned 2.56. Any clue why and when that happens? Is it error or am I missing something about banker's rounding? Thanks.. 回答1: As Matthew said, 18.565 can't be accurately represented. The actual value used is 18.565000000000001278976924368180334568023681640625 (found using DoubleConverter), which is clearly beyond half-way. Now I've a

Rounding to even in C#

孤者浪人 提交于 2019-12-21 09:19:13
问题 I'm not seeing the result I expect with Math.Round. return Math.Round(99.96535789, 2, MidpointRounding.ToEven); // returning 99.97 As I understand MidpointRounding.ToEven, the 5 in the thousandths position should cause the output to be 99.96. Is this not the case? I even tried this, but it returned 99.97 as well: return Math.Round(99.96535789 * 100, MidpointRounding.ToEven)/100; What am I missing Thanks! 回答1: You're not actually at the midpoint. MidpointRounding.ToEven indicates that if you