c# - 将任何 n 位数字四舍五入为 (n-1) 个零位

标签 c# vb.net rounding

抱歉很难表述。

我需要像这样四舍五入:

12 -> 10
152 -> 200
1538 -> 2000
25000 -> 30000
etc. 

绞尽脑汁,但不知道该怎么做。必须适用于任意 n 个数字。任何人都有一个优雅的方法吗?

c# 或 vb.net

最佳答案

这个怎么样:

        double num = 152;

        int pow = (int)Math.Log10(num);

        int factor = (int)Math.Pow(10, pow);

        double temp = num / factor;

        double result = Math.Round(temp) * factor;

关于c# - 将任何 n 位数字四舍五入为 (n-1) 个零位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4601228/

相关文章:

c# - 如何使用 C# 和 LINQ 深入提取 XML 中的信息?

javascript - 合并来自用户控件的 pageLoad() 代码?

c# - 返回 IList 与 List 或 IEnumerable 与 List<Class> 之间有什么区别。我想知道返回哪个更好

SQL 语句作为列表框 VB.NET 的数据源

c# - 使用 Math.Round 的舍入问题

floating-point - 使用 RapidJSON 舍入 double 类型

C# Entity Framework 延迟加载未加载

c# - 覆盖 Asp.net WebControls、UserControls、Controls 上的 Render 方法时的最佳实践

java - Android Studio 中舍入的观察结果 - java.以及一些预期的实际解释

c# - Azure 表 : Unable to load one or more of the requested types. 检索 LoaderExceptions 属性以获取更多信息