English 中文(简体)
如何与C#中最接近的整数进行回合
原标题:How to Round to the nearest whole number in C#
  • 时间:2012-01-13 01:11:25
  •  标签:
  • c#
  • rounding

我怎么能够把价值观四舍五入到最接近的愤怒之中?

例如:

1.1 => 1
1.5 => 2
1.9 => 2

"Math.Ceiling()" is not helping me. Any ideas?

最佳回答

See Official documentation for more. 例如:

基本上,请见<代码>Math.Round方法。

  1. The value you want to round.
  2. The number of decimals you want to keep after the value.
  3. An optional parameter you can invoke to use AwayFromZero rounding. (ignored unless rounding is ambiguous, e.g. 1.5)

样本代码:

var roundedA = Math.Round(1.1, 0); // Output: 1
var roundedB = Math.Round(1.5, 0, MidpointRounding.AwayFromZero); // Output: 2
var roundedC = Math.Round(1.9, 0); // Output: 2
var roundedD = Math.Round(2.5, 0); // Output: 2
var roundedE = Math.Round(2.5, 0, MidpointRounding.AwayFromZero); // Output: 3
var roundedF = Math.Round(3.49, 0, MidpointRounding.AwayFromZero); // Output: 3

rel=“noreferer”>Live Demo

页: 1 AwayFromZero,如果你想要四舍五入,价值为5。 遗憾的是,这只是<代码>Math.Round()的缺省行为。 如果使用<代码>MidpointRounding。 ToEven(缺省),该数值四舍五入至最接近的even/strong>(1.5<>>>>>>>,四舍五入至2,但2.5<>>>>>>>也四舍五入至<2>>>。

问题回答
Math.Ceiling

总是重整(上限)

Math.Floor

总是向下(向上)。

你们之后的只是什么。

Math.Round

https://stackoverflow.com/questions/977796/in-c-math-round2-5-result-is-2-inoul-of-3-are-you-kivention-me> 该员额

页: 1 <代码>Ceiling总“rounds”上,而Round则依次点后数值上加或缩小。

there s this manual, and kinda cute way too:

double d1 = 1.1;
double d2 = 1.5;
double d3 = 1.9;

int i1 = (int)(d1 + 0.5);
int i2 = (int)(d2 + 0.5);
int i3 = (int)(d3 + 0.5);

仅增加任何数字的0.5,将其输入到(或用在地)上,并将在数学上正确四舍五入。

You can use Math.Round as others have suggested (recommended), or you could add 0.5 and cast to an int (which will drop the decimal part).

double value = 1.1;
int roundedValue = (int)(value + 0.5); // equals 1

double value2 = 1.5;
int roundedValue2 = (int)(value2 + 0.5); // equals 2

只是提醒。 两倍。

Math.Round(0.3 / 0.2 ) result in 1, because in double 0.3 / 0.2 = 1.49999999
Math.Round( 1.5 ) = 2

你有马蒂。 确实是你想要的圆桌活动。

Math.Round(1.1) results with 1
Math.Round(1.8) will result with 2.... and so one.

如果已经存在5个分歧的话,这将四舍五入。

public static double R(double x)
{
    // markup to nearest 5
    return (((int)(x / 5)) * 5) + ((x % 5) > 0 ? 5 : 0);
}

I was looking for this, but my example was to take a number, such as 4.2769 and drop it in a span as just 4.3. Not exactly the same, but if this helps:

Model.Statistics.AverageReview   <= it s just a double from the model

然后:

@Model.Statistics.AverageReview.ToString("n1")   <=gives me 4.3
@Model.Statistics.AverageReview.ToString("n2")   <=gives me 4.28

etc...

采用<代码>Math.Round(序号) 发至最接近的整数。

var roundedVal = Math.Round(2.5, 0);

其结果如下:

var roundedVal = 3

如果你与愤怒者而不是浮点数合作,那么,就是这样。

#define ROUNDED_FRACTION(numr,denr) ((numr/denr)+(((numr%denr)<(denr/2))?0:1))

这里的numr”denr"均为未签名的ger。

撰写你自己的圆环。 类似,

function round(x) rx = Math.ceil(x) if (rx - x <= .000001) return int(rx) else return int(x) end

这是我实际寻求的。

float myRound(float numberIn, int numDig) 
{ 
    // float numberIn: number to round
    // int numDig: number of digits after the point
    // myRound(123.456,2) => 123.45
    // myRound(123.456,1) => 123.4
    // myRound(123.456,-1) => 120
    float tenPow = Mathf.Pow(10, numDig);
    return ((int)tenPow*numberIn)/tenPow;
}

这里是正常四舍五入、四舍五入和四舍五入的范例。

                        if (Overtime_Policy == "Overtime, Normal Rounding") { Calculated_Hours = Convert.ToInt32(Math.Round(UnRounded_Hours, 0, MidpointRounding.AwayFromZero)); }
                        if (Overtime_Policy == "Overtime, Round Down") { Calculated_Hours = Convert.ToInt32(Math.Truncate(UnRounded_Hours)); }
                        if (Overtime_Policy == "Overtime, Round Up") { Calculated_Hours = Convert.ToInt32(Math.Ceiling(UnRounded_Hours)); }
decimal RoundTotal = Total - (int)Total;
if ((double)RoundTotal <= .50)
   Total = (int)Total;
else
   Total = (int)Total + 1;
lblTotal.Text = Total.ToString();




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...