Math.round不适用于1.44 - > 1.50 [英] Math.round is not working for 1.44-->1.50

查看:72
本文介绍了Math.round不适用于1.44 - > 1.50的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要的是用户输入是1.53意味着输出将来自1.50,如下面的情况



用户输入预期输出

1.20 1.50
1.50 1.50
1.60 2.00







如何实现这个输出



我尝试过:



i有试过数学。圆形Math.Round(10.45,1,MidpointRounding.ToEven)但我无法获得输出







我使用以下方法找到了解决方案



  double  Expected_result1 =  25  51 ; 
value = Expected_result1 - Math.Floor(Expected_result1); // 获得分数
Expected_result1 =(( value > = 0 01 )&&( value < = 0 50 ))? (Math.Floor(Expected_result1)+ 0 5 ):Math.Ceiling(Expected_result1);

解决方案

这不是 Math.Round 的工作方式。通常,舍入是在特定的小数位置完成的。因此,如果为舍入目的指定1位小数,那么基本上答案将是最接近的1位小数。



然后你有一些选项可以指定中点舍入。这对于财务应用程序来说通常更有用,可以减少累积错误。



如果你想要舍入到0.5这样的特定倍数,那么你必须添加一些额外的代码。例如,您可以乘以2,舍入为单位,然后除以2.



这是代码,每行有一个操作,注释中有值。 />


  var   value  =  10  45 ;  //   10.45  
value * = 2 0 ; // 20.90
value = Math.Round( value 0 ); // 21.00
value / = 2 0 ; // 10.50





但是,你怎么看待1.20四舍五入到1.50。使用上面的代码,它将舍入到1.00。用 Math.Ceiling 替换 Math.Round 可能是你想要的(但是,你不能指定小数位数) 。



或者,你可能想在调用 Math.Round 之前加0.5。在这种情况下,中点舍入可能更重要。



有关舍入如何工作的信息,阅读文档可能很有用,因为它在文档中非常清楚: MidpointRounding Enumeration(System) [ ^ ]


my need is the user input is 1.53 means the output will come 1.50 like below cases

user input       Expectedoutput

1.20          1.50
1.50          1.50
1.60          2.00




how to achive this output

What I have tried:

i have tried math. round Math.Round(10.45,1,MidpointRounding.ToEven) but i could not achive the output



I have found the solution using below method

double  Expected_result1 = 25.51;
value = Expected_result1 - Math.Floor(Expected_result1);// get fraction
Expected_result1 = ((value >= 0.01) && (value <= 0.50)) ? (Math.Floor(Expected_result1) + 0.5) : Math.Ceiling(Expected_result1);

解决方案

This is not how Math.Round works. Usually, rounding is done at a specific decimal position. So if you specify 1 decimal for rounding purpose, then essentially the answer would be the nearest number with 1 decimal.

Then you have some options to specify mid-point rounding. This is generally more useful for financial applications to reduce cumulative error.

If you want to round to a specific multiple like 0.5, then you have to add some extra code. For example, you might multiply by 2, round to unit and then divide by 2.

Here is the code with one operation per line and value in comment.

var value = 10.45;             // 10.45
value *= 2.0;                  // 20.90
value = Math.Round(value, 0);  // 21.00
value /= 2.0;                  // 10.50



However, how do you expect 1.20 to be rounded to 1.50. With above code, it will round to 1.00. Replacing Math.Round by Math.Ceiling might be what you want (however, you cannot specify the number of decimals).

Alternatively, you might want to add 0.5 before calling Math.Round. In that case, mid-point rounding might be more important.

For information on how rounding works, it might be useful to read documentation as it is very clear in documentation:MidpointRounding Enumeration (System)[^]


这篇关于Math.round不适用于1.44 - > 1.50的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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