如何圆没有math.rule? [英] how to round without math.rule?

查看:103
本文介绍了如何圆没有math.rule?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我有一个问题。我需要将数字乘以0.5。

例如,

15.234应该是15

15.617应该是15.5

15. 256应该是15.5

15.25应该是15.5

15.880应该是16

15.7777应该是16

所以,我看一下1/4我的小数部分。如果我在中间点有一个数字我必须向上舍入,但我可以得到3,4位及更多位数的小数部分。



谢谢

解决方案

最简单的解决方案可能是:

 功能 Round( ByVal  As   Double  As   Double  
返回 Math.Round(值* 2 ,MidpointRounding.AwayFromZero)/ 2
结束 功能

Dim values() As Double = { 15 234 15 617 15 256 15 25 15 880 15 7777 }

对于 每个 v As Double In values
Console.WriteLine (回合(v))
下一步

' 输出:
' 15
' 15.5
' 15.5
' 15.5
' 16
' 16


你只是说你要将所有这些数字舍入到最接近的0.5?不,这不是标准 Math 方法的工作方式。你必须开发自己的舍入方法。 (顺便说一下,为什么这些舍入规则?)



所以,你将自己开发适当的算术。我可以给你一个建议,它可以让你在你自己的方法的核心使用标准的 Math.Round 。这是一个想法:做坐标变换:将所有数字除以0.5(即乘以2),然后在双尺度坐标系中进行所有中间计算。换句话说,在单位为0.5的位置进行所有计算,所有数字都是0.5个单位的数字。然后舍入将减少到舍入到最接近的1.最后,当你呈现结果时,你可以回到原始系统我将结果乘以0.5。



或者,您可以自己完成所有计算,从分离数字的小数部分和整数部分开始。反过来,你可以自己做,或者你可以使用方法

Math.Floor 和/或数学.Ceiling 。换句话说,你需要的只是一点逻辑和数学思考。



-SA


< blockquote>我知道你想要舍入到最近的 0.5 而不使用 System.Math

  double  [] values =  new  [] { 15  234  15  617  15  256  15  25  15  880  15  7777 }; 

foreach double v in values)
Console.WriteLine( {0}舍入到{1},v,Round(v));

double Round( double
{
return (( int )((值+ 0。 251 )* 2))/ 2. 0 ;
}



输出:

 15.234轮到15 
15.617轮到15.5
15.256轮到15.5
15.25轮到15.5
15.88轮到16
15.7777轮到16



我相信VB.NET,它会像一样

  Dim 值( )作为  Double  = { 15 。< span class =code-digit> 234 , 15  617  15  256  15  25  15  880  15  7777 }; 

对于 每个 v As Double
Console.WriteLine(Round(v))
下一步

功能 Round( ByVal As Double As Double
Round = CInt (((值+ 0。 251 )* 2))/ 2.0f
结束 功能


Hello, I have a question. I have to round number by 0.5.
for example,
15.234 should be 15
15.617 should be 15.5
15. 256 should be 15.5
15.25 should be 15.5
15.880 should be 16
15.7777 should be 16
So, I look in which 1/4 my fractional part is presented. If i have a number in the middle point i have to round up, but I can have fractional parts from 3, 4 and more digits.

thanks

解决方案

The simplest solution would probably be:

Function Round(ByVal value As Double) As Double
    Return Math.Round(value * 2, MidpointRounding.AwayFromZero) / 2
End Function

Dim values() As Double = { 15.234, 15.617, 15.256, 15.25, 15.880, 15.7777 }
 
For Each v As Double In values
    Console.WriteLine(Round(v))
Next

' Output:
' 15
' 15.5
' 15.5
' 15.5
' 16
' 16


Are you just saying that you want to round up all those numbers "to nearest 0.5"? No, this is not how standard Math methods work. You have to develop your own rounding method. (By the way, why these rounding rules?)

So, you are going to develop appropriate arithmetic yourself. I can give you one advice which could allow you to use standard Math.Round at the core of your own method. This is the idea: do the coordinate transform: divide all you numbers by 0.5 (that is, multiply by 2) and do all your intermediate calculations in that "double-scaled" coordinate system. In other words, do all the calculations in the where the unit is 0.5, and all numbers are "numbers of 0.5 units". Then the rounding will be reduced to the rounding to nearest 1. At the very end, when you present the results, you can get back to original system my multiplying the results by 0.5.

Alternatively, you can do all the calculations by yourself, starting from separating fractional and integer part of the number. In turn, you can do it by yourself, or you can use the methods
Math.Floor and/or Math.Ceiling. In other words, all you need is a bit of logical and mathematical thinking.

—SA


I understand you want to round to the nearest 0.5 without using System.Math.

double[] values = new[] { 15.234, 15.617, 15.256, 15.25, 15.880, 15.7777 };

foreach (double v in values)
    Console.WriteLine("{0} rounds to {1}", v, Round(v));

double Round(double value)
{
    return ((int)((value+0.251)*2))/2.0;   
}


Outputs:

15.234 rounds to 15
15.617 rounds to 15.5
15.256 rounds to 15.5
15.25 rounds to 15.5
15.88 rounds to 16
15.7777 rounds to 16


I believe in VB.NET, it would be something like:

Dim values() As Double = { 15.234, 15.617, 15.256, 15.25, 15.880, 15.7777 };

For Each v As Double In values
    Console.WriteLine(Round(v))
Next

Function Round(ByVal value As Double) As Double
    Round = CInt(((value+0.251)*2))/2.0f
End Function


这篇关于如何圆没有math.rule?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆