Advertisement
VladoG

Differences between Math.Round, Truncate, Ceiling, Floor

Oct 31st, 2017
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.57 KB | None | 0 0
  1. using System;
  2.  
  3. namespace ConsoleApp_dotNetFramework
  4. {
  5.     class Roundings
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             double a = 123.321;
  10.             double b = 123.456;
  11.             double c = 123.556;
  12.             double d = 123.789;
  13.             double e = -123.789;
  14.             double f = -124.2;
  15.  
  16.             Console.WriteLine("Math.Truncate:");
  17.             Console.WriteLine("Truncate rounds <a, b, c, d> to the nearest integer towards ZERO.");
  18.             Console.WriteLine("Math.Truncate eliminates numbers after the decimal. It acts upon a decimal or floating-point number. It calculate the integral part of a number. Math.Truncate is reliable and easy-to-use. Its functionality differs from Math.Round.");
  19.             Console.WriteLine($"a = {a} -Truncate-> {Math.Truncate(a)}");
  20.             Console.WriteLine($"b = {b} -Truncate-> {Math.Truncate(b)}");
  21.             Console.WriteLine($"c = {c} -Truncate-> {Math.Truncate(c)}");
  22.             Console.WriteLine($"d = {d} -Truncate-> {Math.Truncate(d)}");
  23.             Console.WriteLine($"e = {e} -Truncate-> {Math.Truncate(e)}");
  24.             Console.WriteLine($"f = {f} -Truncate-> {Math.Truncate(f)}");
  25.  
  26.             Console.WriteLine("--------------------------------------------------------------------");
  27.  
  28.             Console.WriteLine("Math.Floor:");
  29.             Console.WriteLine("Returns the largest integer less than or equal to the specified double-precision floating-point number.");
  30.             Console.WriteLine("Math.Floor rounds down. It operates on types such as decimal or double. It reduces the value to the nearest integer. The Floor method is straightforward, but useful, when it is called for in programming.");
  31.             Console.WriteLine($"a = {a} -Floor-> {Math.Floor(a)}");
  32.             Console.WriteLine($"b = {b} -Floor-> {Math.Floor(b)}");
  33.             Console.WriteLine($"c = {c} -Floor-> {Math.Floor(c)}");
  34.             Console.WriteLine($"d = {d} -Floor-> {Math.Floor(d)}");
  35.  
  36.             Console.WriteLine("--------------------------------------------------------------------");
  37.  
  38.             Console.WriteLine("Math.Ceiling:");
  39.             Console.WriteLine("Returns the smallest integral value that is greater than or equal to the specified double-precision floating-point number.");
  40.             Console.WriteLine("Math.Ceiling rounds up to the next full integer. This means that any number over 1 but under 2 would be rounded to 2.");
  41.             Console.WriteLine($"a = {a} -Ceiling-> {Math.Ceiling(a)}");
  42.             Console.WriteLine($"b = {b} -Ceiling-> {Math.Ceiling(b)}");
  43.             Console.WriteLine($"c = {c} -Ceiling-> {Math.Ceiling(c)}");
  44.             Console.WriteLine($"d = {d} -Ceiling-> {Math.Ceiling(d)}");
  45.  
  46.             Console.WriteLine("--------------------------------------------------------------------");
  47.  
  48.             Console.WriteLine("Math.Round:");
  49.             Console.WriteLine("Rounds a value to the NEAREST integer or to the specified number of fractional digits.");
  50.             Console.WriteLine("Math.Round. This method rounds numbers to the nearest value. It receives the desired number of significant digits. It is part of the System namespace.");
  51.             Console.WriteLine($"a = {a} -Round-> {Math.Round(a)}");
  52.             Console.WriteLine($"b = {b} -Round-> {Math.Round(b)}  <--- Attention!");
  53.             Console.WriteLine($"c = {c} -Round-> {Math.Round(c)}");
  54.             Console.WriteLine($"d = {d} -Round-> {Math.Round(d)}");
  55.  
  56.             Console.WriteLine("--------------------------------------------------------------------");
  57.         }
  58.     }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement