C#Math.Round(double,Int32)麻烦 [英] C# Math.Round(double,Int32) Troubles

查看:111
本文介绍了C#Math.Round(double,Int32)麻烦的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Visual C#2010 Express,并被Math.Round方法困住了.

两个重载对于我的问题很重要:
1- Math.Round(十进制d,int n)
2- Math.Round(double d,int n)

我正尝试将5个 个基数舍入为5个基数.请参见下面的代码段:

I am using Visual C# 2010 Express and am stumped with the Math.Round method.

Two overloads are important to the context of my question:
1- Math.Round(decimal d, int n)
2- Math.Round(double d, int n)

I am trying to round to 5 total base-10 digits. See code snippet below:

int fractional_digits = 4;
double number_to_round = 123.456789;
double rounded_result = Math.Round(number_to_round, fractional_digits);
number_to_round = 11223344.556677;
rounded_result = Math.Round(number_to_round, fractional_digits);
number_to_round = 876.54321E+57;
rounded_result = Math.Round(number_to_round, fractional_digits);



该代码是多余的,无法多次显示该问题.第一个四舍五入的结果是123.4568而不是123.46(或1.2346E + 2)(1个整数+ 4个小数个数字).

第二个舍入结果为11223344.5567,而不是11223000(或1.1223E + 7).
最后,四舍五入的结果是8.7654321E + 59而不是8.7654E + 59.

我只需要5位有效数字.如上所示,该代码示例似乎正在使用方法 1 -不是方法 2 .

我需要方法2.(请参见下面复制的评论标题).



The code is redundant to show the issue three times. The first rounded result gives 123.4568 not 123.46 (or 1.2346E+2) (1 integer + 4 fractional digits).

The second rounded result is 11223344.5567 instead of 11223000 (or 1.1223E+7).
Lastly, the rounded result is 8.7654321E+59 instead of 8.7654E+59.

I need only 5 significant digits. The code example seems to be using method 1 as shown above - not method 2.

I need method 2. (See comment header copied below).

//
// Summary:
//     Rounds a double-precision floating-point value to a specified number of fractional
//     digits.
//
// Parameters:
//   value:
//     A double-precision floating-point number to be rounded.
//
//   digits:
//     The number of fractional digits in the return value.
//
// Returns:
//     The number nearest to value that contains a number of fractional digits equal
//     to digits.
//
// Exceptions:
//   System.ArgumentOutOfRangeException:
//     digits is less than 0 or greater than 15.
public static double Round(double value, int digits);



如何强制编译器使用方法2?还有其他建议吗?

我已经在这个网站上(通过Google)进行了基本搜索以寻求帮助,但是所有示例仅在整数位置显示了单个位置值.

提前谢谢.

-EJ



How do I force the compiler to use method 2? Any other suggestions?

I have done a basic search for help on this site (via Google) but all examples only show a single place-value in the integer position.

Thanks in advance.

-EJ

推荐答案

可能有用的一些想法:

双精度不是十进制数字的精确表示,它是一个近似值,因此您不一定总是得到想要的结果,只是它可以表示的最接近的结果.

对于小于100,000的数字,您可以轻松确定所需的小数位数:
A few thoughts that might help:

A double is not an exact representation of a decimal number, it''s an approximation, so you might not always get the result you want, just the closest result it can represent.

For numbers less than 100,000, you can easily determine the number of fractional digits you need:
int fractional_digits =  5 - (int)Math.Floor(Math.Log10(number_to_round)) + 1;


(是的,您可以只使用4-...,但是我这样写是为了更清楚地知道它是如何工作的.)
Math.Floor(Math.Log10(number_to_round))给出的数字要比小数点前的位数少一位(这很有意义,如果您考虑对数和数字如何工作),请从总数中减去该位数,然后得到所需的位数小数点.这甚至适用于值< 1.

我不确定用哪种明显的方法来处理小数点前5位以上的数字,也许将它们除以10的幂以使其更短,然后取整,然后再乘以10的幂?


(Yes, you could use just 4 - ... instead, but I wrote it this way to make it more clear how it works.)
Math.Floor(Math.Log10(number_to_round)) gives you one less than the number of digits the number has before the decimal place (it makes sense, if you think about how logs and digits work), subtract that from the total and you get the number of digits you want after the decimal point. This will even work for values < 1.

I''m not sure of an obvious way to deal with numbers with more than 5 digits before the decimal point, maybe divide them by a power of 10 to make them shorter, then round, then multiply them by that power of 10 again?


基于lewax00的答案,我构建了一个解决方法",如下所示:

Building on the answer by lewax00, I have built a "workaround" as shown below:

int fractional_digits = 4;
double number_to_round = 123.456789;
double rounded_result;
double base_ten_factor = 0;

base_ten_factor = Math.Pow(10,(int)Math.Floor(Math.Log10(number_to_round)));
number_to_round = Math.Round(number_to_round / base_ten_factor, fractional_digits);
rounded_result = number_to_round * base_ten_factor;
number_to_round = 11223344.556677;

base_ten_factor = Math.Pow(10, (int)Math.Floor(Math.Log10(number_to_round)));
number_to_round = Math.Round(number_to_round / base_ten_factor, fractional_digits);
rounded_result = number_to_round * base_ten_factor;



第一个结果是123.46,第二个结果是11223000.正是我所需要的.

感谢lewax00帮助我度过了精神上的困境.

-EJ



The first result is 123.46 and the second is 11223000. Just what I need.

Thanks lewax00 for getting me through the mental log-jam.

-EJ


这篇关于C#Math.Round(double,Int32)麻烦的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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