Advertisement
elena1234

Students- how to round in StringBuilder

Feb 10th, 2021
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.68 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace Students
  7. {
  8.     public class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             var listOfStudents = new List<Student>();
  13.             int numberOfStudents = int.Parse(Console.ReadLine());
  14.             for (int i = 0; i < numberOfStudents; i++)
  15.             {
  16.                 string[] inputArray = Console.ReadLine().Split(" ", StringSplitOptions.RemoveEmptyEntries);
  17.                 string firstName = inputArray[0];
  18.                 string lastName = inputArray[1];
  19.                 double grade = double.Parse(inputArray[2]);
  20.                 var newStudent = new Student(firstName, lastName, grade);
  21.                 listOfStudents.Add(newStudent);
  22.             }
  23.  
  24.             foreach (var student in listOfStudents.OrderByDescending(s=>s.Grade))
  25.             {
  26.                 Console.WriteLine(student.ToString());
  27.             }
  28.         }
  29.     }
  30.  
  31.  
  32.     class Student
  33.     {
  34.         public Student(string firstName, string lastName,double grade)
  35.         {
  36.             this.FirstName = firstName;
  37.             this.LastName = lastName;
  38.             this.Grade = grade;
  39.         }
  40.  
  41.         public string FirstName { get; set; }
  42.         public string LastName { get; set; }
  43.         public double Grade { get; set; }
  44.  
  45.         public override string ToString()
  46.         {
  47.             var sb = new StringBuilder();
  48.             sb.Append(this.FirstName);
  49.             sb.Append(" ");
  50.             sb.Append(this.LastName);
  51.             sb.Append(": ");
  52.             sb.Append(this.Grade.ToString("N2")); // rounded
  53.             return sb.ToString();
  54.         }
  55.     }
  56. }
  57.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement