+ 2
How can I round off number to nearest whole numberin c#?
6 Antworten
+ 3
Sakeef Hasan
Use Math.Round(n) for round off n,
ex: 1.2 to 1.0, and 1.7 to 2.0
Use Math.Ceiling(n) for round up n,
Ex: 1.2 to 2.0
Use Math.Floor(n) for round down n
ex: for 1.2 to 1.0
+ 3
Jayakrishna🇮🇳 Thank you bro ❤️
+ 1
Jayakrishna🇮🇳
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SoloLearn
{
class Program
{
static void Main(string[] args)
{
int houses;
houses = Convert.ToInt32(Console.ReadLine());
//your code goes here
double x =((2*100)/houses);
int n = Math.Round(x);
Console.WriteLine(n);
}
}
}
This is the Halloween candy code,
Bro can you please correct my mistake.
+ 1
There asked to round up result so asking use Ceiling(x) ;
Try
Console.Write ( (int) Math.Ceiling(x)) ;
+ 1
Note that Math.Round uses banker’s rounding; i.e. for a number ending in 5, it rounds to the nearest even number. If you want to round up each time you get a 5, you have to use the flag MidpointRounding.AwayFromZero when you call the method.
0
Add 1