📄 13.2.1.htm
字号:
Paragraph 2</span><span class="paragraph"><span class="sentence"><span class="sentence-number">1</span> <a name="P2S1"></a>Because the explicit conversions include all implicit and explicit numeric conversions, it is always possible to convert from any <span class="non-terminal"><a href="11.1.htm#numeric-type">numeric-type</a></span> to any other <span class="non-terminal"><a href="11.1.htm#numeric-type">numeric-type</a></span> using a cast expression (<a href="14.6.6.htm">§14.6.6</a>).</span> </span><span class="locator">
Paragraph 3</span><span class="paragraph"><span class="sentence"><span class="sentence-number">1</span> <a name="P3S1"></a>The explicit numeric conversions possibly lose information or possibly cause exceptions to be thrown.</span> <span class="sentence"><span class="sentence-number">2</span> <a name="P3S2"></a>An explicit numeric conversion is processed as follows: </span><ul><li><span class="sentence"><span class="sentence-number">3</span> <a name="P3S3"></a> For a conversion from an integral type to another integral type, the processing depends on the overflow checking context (<a href="14.5.12.htm">§14.5.12</a>) in which the conversion takes place: </span></li><ul><li><span class="sentence"><span class="sentence-number">4</span> <a name="P3S4"></a> In a checked context, the conversion succeeds if the value of the source operand is within the range of the destination type, but throws a System.OverflowException if the value of the source operand is outside the range of the destination type.</span> </li><li><span class="sentence"><span class="sentence-number">5</span> <a name="P3S5"></a> In an unchecked context, the conversion always succeeds, and proceeds as follows: </span></li><ul><li><span class="sentence"><span class="sentence-number">6</span> <a name="P3S6"></a> If the source type is larger than the destination type, then the source value is truncated by discarding its "extra" most significant bits.</span> <span class="sentence"><span class="sentence-number">7</span> <a name="P3S7"></a>The result is then treated as a value of the destination type.</span> </li><li><span class="sentence"><span class="sentence-number">8</span> <a name="P3S8"></a> If the source type is smaller than the destination type, then the source value is either sign-extended or zero-extended so that it is the same size as the destination type.</span> <span class="sentence"><span class="sentence-number">9</span> <a name="P3S9"></a>Sign-extension is used if the source type is signed; zero-extension is used if the source type is unsigned.</span> <span class="sentence"><span class="sentence-number">10</span> <a name="P3S10"></a>The result is then treated as a value of the destination type.</span> </li><li><span class="sentence"><span class="sentence-number">11</span> <a name="P3S11"></a> If the source type is the same size as the destination type, then the source value is treated as a value of the destination type.</span> </li></ul></ul><li><span class="sentence"><span class="sentence-number">12</span> <a name="P3S12"></a> For a conversion from <span class="keyword">decimal</span> to an integral type, the source value is rounded towards zero to the nearest integral value, and this integral value becomes the result of the conversion.</span> <span class="sentence"><span class="sentence-number">13</span> <a name="P3S13"></a>If the resulting integral value is outside the range of the destination type, a System.OverflowException is thrown.</span> </li><li><span class="sentence"><span class="sentence-number">14</span> <a name="P3S14"></a> For a conversion from <span class="keyword">float</span> or <span class="keyword">double</span> to an integral type, the processing depends on the overflow-checking context (<a href="14.5.12.htm">§14.5.12</a>) in which the conversion takes place: </span></li><ul><li><span class="sentence"><span class="sentence-number">15</span> <a name="P3S15"></a> In a checked context, the conversion proceeds as follows: </span></li><ul><li><span class="sentence"><span class="sentence-number">16</span> <a name="P3S16"></a> The value is rounded towards zero to the nearest integral value.</span> <span class="sentence"><span class="sentence-number">17</span> <a name="P3S17"></a>If this integral value is within the range of the destination type, then this value is the result of the conversion.</span> </li><li><span class="sentence"><span class="sentence-number">18</span> <a name="P3S18"></a> Otherwise, a System.OverflowException is thrown.</span> </li></ul><li><span class="sentence"><span class="sentence-number">19</span> <a name="P3S19"></a> In an unchecked context, the conversion always succeeds, and proceeds as follows: </span></li><ul><li><span class="sentence"><span class="sentence-number">20</span> <a name="P3S20"></a> The value is rounded towards zero to the nearest integral value.</span> <span class="sentence"><span class="sentence-number">21</span> <a name="P3S21"></a>If this integral value is within the range of the destination type, then this value is the result of the conversion.</span> </li><li><span class="sentence"><span class="sentence-number">22</span> <a name="P3S22"></a> Otherwise, the result of the conversion is an unspecified value of the destination type.</span> </li></ul></ul><li><span class="sentence"><span class="sentence-number">23</span> <a name="P3S23"></a> For a conversion from <span class="keyword">double</span> to <span class="keyword">float</span>, the <span class="keyword">double</span> value is rounded to the nearest <span class="keyword">float</span> value.</span> <span class="sentence"><span class="sentence-number">24</span> <a name="P3S24"></a>If the <span class="keyword">double</span> value is too small to represent as a <span class="keyword">float</span>, the result becomes positive zero or negative zero.</span> <span class="sentence"><span class="sentence-number">25</span> <a name="P3S25"></a>If the <span class="keyword">double</span> value is too large to represent as a <span class="keyword">float</span>, the result becomes positive infinity or negative infinity.</span> <span class="sentence"><span class="sentence-number">26</span> <a name="P3S26"></a>If the <span class="keyword">double</span> value is NaN, the result is also NaN.</span> </li><li><span class="sentence"><span class="sentence-number">27</span> <a name="P3S27"></a> For a conversion from <span class="keyword">float</span> or <span class="keyword">double</span> to <span class="keyword">decimal</span>, the source value is converted to <span class="keyword">decimal</span> representation and rounded to the nearest number after the 28th <span class="keyword">decimal</span> place if required (<a href="11.1.6.htm">§11.1.6</a>).</span> <span class="sentence"><span class="sentence-number">28</span> <a name="P3S28"></a>If the source value is too small to represent as a <span class="keyword">decimal</span>, the result becomes zero.</span> <span class="sentence"><span class="sentence-number">29</span> <a name="P3S29"></a>If the source value is NaN, infinity, or too large to represent as a <span class="keyword">decimal</span>, a System.OverflowException is thrown.</span> </li><li><span class="sentence"><span class="sentence-number">30</span> <a name="P3S30"></a> For a conversion from <span class="keyword">decimal</span> to <span class="keyword">float</span> or <span class="keyword">double</span>, the <span class="keyword">decimal</span> value is rounded to the nearest <span class="keyword">double</span> or <span class="keyword">float</span> value.</span> <span class="sentence"><span class="sentence-number">31</span> <a name="P3S31"></a>While this conversion may lose precision, it never causes an exception to be thrown.</span> </li></ul></span><span class="ruler"></span><table><tr><td><table align="left" bgcolor="navy"><tr bgcolor="navy"><td><font face="Arial,sans-serif" size="6" color="yellow"><strong>{ JSL }</strong></font></td></tr></table></td></tr><tr><td><font face="Arial,sans-serif" size="2" color="navy"><strong>Jagger Software Ltd</strong></font></td></tr><tr><td><font face="Arial,sans-serif" size="2" color="navy"><strong>Company # 4070126</strong></font></td></tr><tr><td><font face="Arial,sans-serif" size="2" color="navy"><strong>VAT # 762 5213 42</strong></font></td></tr></table><img src="valid-html401.png" align="left" height="31" width="88" alt="Valid HTML 4.01" /><img src="vcss.gif" align="left" height="31" width="88" alt="Valid CSS" /></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -