我有一个要与您讨论的问题,可能是您有解决问题的方法,实际上在销售点,结帐时我有小数位数,我想四舍五入,例如 示例: 案例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和适当的格式说明符: msdn.microsoft/en-us/library/fzeeb5cd.aspx [ ^ ], msdn.microsoft/en-us/library/dwhawy9k.aspx [ ^ ], msdn.microsoft/en-us/library/0c899ak8.aspx [ ^ ]. 基本上,您需要使用带有"0",#"和.''占位符(最后一个链接)的自定义格式.在后台,您的数据将被适当地四舍五入,但是没有任何四舍五入的值潜入计算的风险,因此,不仅更简单,而且更加安全 .
创建自己的舍入方法 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); }
更多推荐
四舍五入十进制值
发布评论