Ami*_*adi -1 .net c# int numbers rounding
我一直在搜索这个问题几个小时,但我找不到答案所以我问它:
我正在寻找一种方法或一些四舍五入25,599,999至25,000,000或25,599,999到30,000,000:
int num1 = 25599999;
Console.WriteLine(RoundDown(num1, 6 /* This is the places (numbers from end that must be 0) */)) // Should return 25,000,000;
Run Code Online (Sandbox Code Playgroud)
或者向上:
int num1 = 25599999;
Console.WriteLine(RoundUp(num1, 6 /* This is the places (numbers from end that must be 0) */)) // Should return 30,000,000;
Run Code Online (Sandbox Code Playgroud)
注意:我不是在寻找一种舍入十进制数的方法.
小智 8
int RoundDown(int num, int digitsToRound)
{
double tmp = num / Math.Pow(10, digitsToRound);
return (int)(Math.Floor(tmp) * Math.Pow(10, digitsToRound));
}
int RoundUp(int num, int digitsToRound)
{
double tmp = num / Math.Pow(10, (digitsToRound + 1)) + 1;
return (int)(Math.Floor(tmp) * Math.Pow(10, (digitsToRound + 1)));
}
Run Code Online (Sandbox Code Playgroud)