1

I am an extreme beginner when it comes to coding, and I am building a batting average calculator as one of my first programs.

Console.WriteLine("Your batting average is: " + (hits / atBats) );

The input for hits is 165 and 419 for atBats, and it calculates to .3937947494. Though this is correct, I would like the number to read as .394. How would I do that?

crtobin34
  • 11
  • 1

3 Answers3

1

A sample example that demonstrates usage of Math.Round() function:

using System;

class MainClass {
public static void Main (string[] args) {
    double result =  0.3937947494;

    Console.WriteLine(Math.Round(result,3));
 }
}

// here result is = 0.3937947494, but you will get output as 0.394 in the console.

VERSION 2:

using System;

public class Program
{
    public static void Main()
    {
        int hits = 165;
        int atBats = 419;
        double result = (double)hits / (double)atBats;
        Console.WriteLine("Your batting average is: " +Math.Round(result,3));
    }
}
// this produces the same result
// Your batting average is: 0.394 

Screenshot for second alternative:

enter image description here

VERSION 3: Serves better readability (as suggested correctly by @Manti_Core

using System;

public class Program
{
    public static void Main()
    {
        double hits = 165;
        double atBats = 419;
        double result = hits/atBats;
        Console.WriteLine("Your batting average is: " + (hits / atBats).ToString("0.000"));
    }
} 

enter image description here

Hope this helps.

Rohan Rao
  • 2,505
  • 3
  • 19
  • 39
0

You can use string.Format:

var number = 0.3937947494;            
Console.WriteLine(string.Format("{0:0.000}", number));
StepUp
  • 36,391
  • 15
  • 88
  • 148
0

You can do simple like below :

double hits = 165;
double atBats = 419;
Console.WriteLine("Your batting average is: " + (hits / atBats).ToString("#.000"));
Console.Read();

here your are simply formatting the value that you want to display.

Performance of string format vs boxing values :

String format String format performance

Boxing values enter image description here

MDT
  • 1,535
  • 3
  • 16
  • 29
  • OP has not mentioned whether hits and atBats are int or double. Looking at the variables hits suggests that it cannot be decimal.. It has to be int. (Because Batting hits cannot be measured in decimal) – Rohan Rao Apr 27 '20 at 06:52
  • but OP has not mentioned the data type.. How about considering the memory optimization? You are using double for something which is not double... It seems that it is int. Since int takes 2 or 4 bytes compared to double which takes 8 bytes choosing correct datatype also plays an important role. You are wasting 4 bytes taking an integer value as double which is certainly not expected. But I am not denying your Readability issue that is lacking in my answer. – Rohan Rao Apr 27 '20 at 07:18
  • @noobprogrammer if OP has not mentioned int or double then i guess it is upto to the us to decide on it, because dividing by ints wont give you the result value like .3937947494 as OP mentions, i guess this partially depends on common sense ? – MDT Apr 27 '20 at 07:22
  • I agree to your point. But taking wrong datatype unnecessarily won't serve the purpose. We need to think from memory perspective as well... I am changing my answer to include the string formatting as well. Now it's upto OP which one to use :D – Rohan Rao Apr 27 '20 at 07:27
  • @noobprogrammer string formatting is already suggested twice in this thread as an answer. – MDT Apr 27 '20 at 07:32