四舍五入十进制值 [英] Round-Off The Decimal Value

查看:101
本文介绍了四舍五入十进制值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个要与您讨论的问题,可能是您有解决问题的方法,实际上在销售点,结帐时我有小数位数,我想四舍五入,例如

示例:
案例1:

如果金额/值大于 ''0'' 或小于 ''4'' 结果为 ''0'' 就像
金额类似于 102.00 103.45 101.66 104 我希望四舍五入后的结果为 100

案例2:

如果金额/值大于 ''4'' 或小于 ''6'' 结果为 ''5'' 就像
金额类似于 104.12 104.00 105.54 105.96 我希望四舍五入后的结果为 105

案例3:

如果金额/值大于 ''6'' 或小于 ''10'' 结果为 ''10'' 就像
金额类似于 106.96 107.00 108.72 109.89/b> 我希望四舍五入后的结果为 110

请解释正确的代码,以便我轻松理解.
谢谢您

I have a problem which I want to Discuss with you, may be you have solution for me, Actually in Point of Sale, I have Decimal Amounts at checkout and I want to Round it off like,

Example:
CASE # 1:

If Amount/Value is Greater than ''0'' or Less than ''4'' I need result as ''0'' like
Amount is like 102.00 OR 103.45 OR 101.66 OR 104 I want Result after Round-Off is 100

CASE # 2:

If Amount/Value is Greater than ''4'' or Less than ''6'' I need result as ''5'' like
Amount is like 104.12 OR 104.00 OR 105.54 OR 105.96 I want Result after Round-Off is 105

CASE # 3:

If Amount/Value is Greater than ''6'' or Less than ''10'' I need result as ''10'' like
Amount is like 106.96 OR 107.00 OR 108.72 OR 109.89/b> I want Result after Round-Off is 110

Please Explain with Proper Code, so It will easy for me to understand it.
Thanks You

推荐答案

我严重怀疑您是否需要舍入任何东西.这是非常非常需要的.看起来您想要一些不同的东西:您只需要以字符串形式提供一些值作为四舍五入的值,仅出于演示目的;这是一个不同的故事.这本身不是四舍五入,而是格式化,更合适的事情.

请使用System.Decimal.ToString和适当的格式说明符:
http://msdn.microsoft.com/en-us/library/fzeeb5cd.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/0c899ak8.aspx [ ^ ].

基本上,您需要使用带有"0",#"和.''占位符(最后一个链接)的自定义格式.在后台,您的数据将被适当地四舍五入,但是没有任何四舍五入的值潜入计算的风险,因此,不仅更简单,而且更加安全 .

—SA
I seriously doubt you need to round anything, ever. This is very, very rarely needed. It looks like you want somewhat different thing: you only need to present some value in a form of a string as a rounded value, for presentation purposes only; and this is a different story. This is not rounding per se but formatting, much more appropriate thing.

Please use System.Decimal.ToString with appropriate format specifier:
http://msdn.microsoft.com/en-us/library/fzeeb5cd.aspx[^],
http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx[^],
http://msdn.microsoft.com/en-us/library/0c899ak8.aspx[^].

Basically, you would need to use the custom format with ''0'', ''#'' and ''.'' placeholders (the last link). Behind the scene, your data will be properly rounded, but without any risk of rounded values sneaking into the calculations, so it''s not only easier, but also much safer.

—SA


创建自己的舍入方法

Create you own round method

private int Round(Decimal x)
{


//decimal x = 100;
            string strTemp = x.ToString();
            strTemp = strTemp.Substring(0, strTemp.IndexOf("."));
            if (Convert.ToInt32(strTemp.Substring(strTemp.Length - 1)) < 4)
            {
              strTemp=  strTemp.Replace(strTemp.Substring(strTemp.Length - 1), "0");
            }
            else if (Convert.ToInt32(strTemp.Substring(strTemp.Length - 1)) > 4 && Convert.ToInt32(strTemp.Substring(strTemp.Length - 1)) < 6)
            {
              strTemp=  strTemp.Replace(strTemp.Substring(strTemp.Length - 1), "5");
            }
            else if (Convert.ToInt32(strTemp.Substring(strTemp.Length - 1)) > 6 && Convert.ToInt32(strTemp.Substring(strTemp.Length - 1)) < 9)
            {
              int temp =  Convert.ToInt32( ((x / 100) / 10) + 1);
              if (temp < 10)
              {
               strTemp=   strTemp.Replace(strTemp.Substring(strTemp.Length - 2), temp.ToString() + "0");
              }
              else
              {
                strTemp=  strTemp.Replace(strTemp.Substring(strTemp.Length - 3), Convert.ToString(((x / 100) + 1)) + "00");
              }
            }
            return Convert.Toint32(strTemp);

}


这篇关于四舍五入十进制值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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