十进制最后一位 [英] ronding last digit of decimal

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

问题描述

我想四舍五入十进制数字的最后一位.如果最后一位数字小于或等于5,则应四舍五入;如果最后一位数字大于5,则应将其四舍五入.例如,如果no为8.23,则应为8.25,如果no是8.27,则应提供值8.29.

最重要的是,我必须在经典ASP 中执行此操作.

在此先谢谢您.

I want to round the last digit of decimal no. If last digit is less than or equal to 5 then it should round to 5, and if the last digit is grater than 5 then it should round it to 9. Like if the no is 8.23 then it should give 8.25, and if the no is 8.27 then it should give value 8.29.

And Most Important is I have to do this in Classic ASP.

Thanks in advance.

推荐答案

我认为您不需要四舍五入任何数值.舍入很少直接调用,也几乎不需要.这是有风险的,因为四舍五入后的值可能会在某些计算中作为中间结果而偶然得到,并导致精度损失.

最有可能的是,您只需要在屏幕上以字符串形式对数字进行舍入表示即可.这与四舍五入不同.为此,请使用string.Format(string format, params object[] values)和适当的格式参数.

请参阅 http://msdn.microsoft.com/en-us/library/system.string .format.aspx [ ^ ].

您将在此处找到有关数字格式字符串的所有信息:
http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/0c899ak8.aspx [ ^ ].

—SA
I don''t think you ever need rounding of any numeric values. Rounding is rarely called directly and is rarely needed. It is risky because a rounded value can accidentally get in some calculations as an intermediate result and cause precision loss.

Most likely, you only need a rounded presentation of a numeric in the form of a string just to be presented on screen. This is not the same as rounding. For this purpose, use string.Format(string format, params object[] values) and appropriate format parameter.

See http://msdn.microsoft.com/en-us/library/system.string.format.aspx[^].

You will find all the information on numeric format strings here:
http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx[^],
http://msdn.microsoft.com/en-us/library/0c899ak8.aspx[^].

—SA


选择Math.Round方法的最适当的重载.

http://msdn.microsoft.com/en-us/library/system.math. round.aspx [^ ]

编辑======================

您为什么取消标记此为答案?完全正确,与SA一样.
Pick the most appropriate overload of the Math.Round method.

http://msdn.microsoft.com/en-us/library/system.math.round.aspx[^]

EDIT ========================

Why did you un-mark this as the answer? It''s completely correct, as is SA''s.


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

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