I need help in writing a program in C# for the problem below. (Rounding to a Spe
ID: 3658982 • Letter: I
Question
I need help in writing a program in C# for the problem below. (Rounding to a Specific Decimal Place) Math. Floor may be used to round a number to a specific decimal place. The statement Y = Math. Floor( x * 10 + 0.5 ) / 10; rounds x to the tenths position ( i.e., the first position to the right of the decimal point). The statement Y = Math. Floor( x * 100 + 0.5 ) / 100; rounds x to the hundredths position ( i.e., the second position to the right of the decimal point). Write an application that defines four methods for rounding a number a number in x in various ways: a. RoundToInterger( number ) b. RoundToTenths( number ) c. RoundToHundredths( number ) d. RoundToThousandths( number ) For each value read, your application should display the original value, the number rounded to the nearest integer, the number rounded to the nearest tenth, the number rounded to the nearest hundredth and the number rounded to the nearest thousandth.Explanation / Answer
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { class RoundingFun { static void Main(string[] args) { double number; Console.Write("Enter a decimal number: "); number=double.Parse(Console.ReadLine()); Console.WriteLine("Rounded to an integer: {0}", RoundToInterger(number)); Console.WriteLine("Rounded to Tenths: {0}",RoundToTenths( number )); Console.WriteLine("Rounded to Hundredths: {0}",RoundToHundredths( number )); Console.WriteLine("Rounded to Thousandths: {0}",RoundToThousandths(number)); Console.ReadLine(); Console.ReadLine(); //stall compiler from closing for 2 line reads }//end main public static int RoundToInterger( double x ) { int n=(int)x; return n; } public static double RoundToTenths( double x ) { return Math. Floor( x * 10 + 0.5 ) / 10; } public static double RoundToHundredths( double x ) { return Math. Floor( x * 100 + 0.5 ) / 100; } public static double RoundToThousandths(double x) { return Math. Floor( x * 1000 + 0.5 ) / 1000; } } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.