我怎么能够把价值观四舍五入到最接近的愤怒之中?
例如:
1.1 => 1
1.5 => 2
1.9 => 2
"Math.Ceiling()" is not helping me. Any ideas?
我怎么能够把价值观四舍五入到最接近的愤怒之中?
例如:
1.1 => 1
1.5 => 2
1.9 => 2
"Math.Ceiling()" is not helping me. Any ideas?
See Official documentation for more. 例如:
基本上,请见<代码>Math.Round方法。
样本代码:
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
页: 1 AwayFromZero,如果你想要四舍五入,价值为5。 遗憾的是,这只是<代码>Math.Round()的缺省行为。 如果使用<代码>MidpointRounding。 ToEven(缺省),该数值四舍五入至最接近的even/strong>(1.5<>>>>>>>,四舍五入至
2
,但2.5<>>>>>>>
也四舍五入至<2>>>。
页: 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(序号) 发至最接近的整数。
http://msdn.microsoft.com/fr-fr/library/system.math.round.aspx” rel=“nofollow”Math.Round
:
double roundedValue = Math.Round(value, 0)
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();
What is the use of default keyword in C#? Is it introduced in C# 3.0 ?
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. ...
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 ...
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 ...
I have two EF entities. One has a property called HouseNumber. The other has two properties, one called StartHouseNumber and one called EndHouseNumber. I want to create a many to many association ...
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, ...
Since I cannot order my dictionary, what is the best way of going about taking key value pairs and also maintaing an index?
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. ...