+ 2

How can I round off number to nearest whole numberin c#?

15th Feb 2022, 6:58 PM
Sakeef Hasan
Sakeef Hasan - avatar
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
15th Feb 2022, 7:46 PM
Jayakrishna 🇮🇳
+ 3
Jayakrishna🇮🇳 Thank you bro ❤️
15th Feb 2022, 8:10 PM
Sakeef Hasan
Sakeef Hasan - avatar
+ 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.
15th Feb 2022, 8:38 PM
Sakeef Hasan
Sakeef Hasan - avatar
+ 1
There asked to round up result so asking use Ceiling(x) ; Try Console.Write ( (int) Math.Ceiling(x)) ;
15th Feb 2022, 8:44 PM
Jayakrishna 🇮🇳
+ 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.
17th Feb 2022, 5:39 AM
JRS
JRS - avatar
0
Add 1
15th Feb 2022, 7:26 PM
Ion Kare
Ion Kare - avatar