Предупреждение: за этот объект проголосовали № 1 Самый Ужасающий Взлом 2008 , таким образом используйте с осторожностью. На самом деле избегайте его как чумы, но это - несомненно Скрытый Ruby.
Когда-нибудь хотят суперсекретный оператор квитирования для некоторой уникальной операции в Вашем коде? Как то, чтобы играть в гольф кода? Попробуйте операторы как - ~ + ~ - или <---, Которые длятся, каждый используется в примерах для инвертирования порядка объекта.
я не имею никакого отношения Проект Superators вне восхищения им.
Here's the complete javascript specification for Math.round(x)
:
15.8.2.15 round (x) Returns the Number value that is closest to x and is equal to a mathematical integer. If two integer Number values are equally close to x, then the result is the Number value that is closer to +∞. If x is already an integer, the result is x.
- If x is NaN, the result is NaN.
- If x is +0, the result is +0.
- If x is −0, the result is −0.
- If x is +∞, the result is +∞.
- If x is −∞, the result is −∞.
- If x is greater than 0 but less than 0.5, the result is +0.
- If x is less than 0 but greater than or equal to -0.5, the result is −0.
NOTE 1 Math.round(3.5) returns 4, but Math.round(–3.5) returns –3.
NOTE 2 The value of Math.round(x) is the same as the value of Math.floor(x+0.5), except when x is −0 or is less than 0 but greater than or equal to -0.5; for these cases Math.round(x) returns −0, but Math.floor(x+0.5) returns +0.
The C# Language Specification does not stipulate any particular rounding algorithm. The closest thing we have is the documentation for .NET's Math.Round
. From that, you can see that some of the javascript cases don't apply (Math.Round
only handles decimals and doubles, not infinity), and the method's overloads give you a lot more control over the result - you can specify the number of fractional digits in the result and the midpoint rounding method. By default, Math.Round
uses 'banker's rounding' (to even).
Округление в ECMAScript в основном является наивным асимметричным округлением (с добавленными проверками для + - Infinity
). WRT, портируя код JavaScript на C #, вероятно, лучше избегать .NET Math.Round
(поскольку он всегда симметричен ) и использовать вместо него Math.Floor
:
double d = -3.5d;
double rounded = Math.Floor(d + 0.5); // -3 matches JavaScript's Math.round
То есть, если вам нужна строгая эмуляция ECMAScript Math.round
.