0

I have declared some values as doubles:

double mean;
double median;

Since the mean after the calculation is a whole number, it is showing without any decimal value. But I need 2 decimal values.
eg:- if the mean is 255, I need to show it as 255.00.
How to achieve this?

What I have tried: Round(mean,2)

Still showing 255

Need 255.00

Need the same for the numbers from the data file too. Need to show the whole number as a decimal number with 2 precision.

wohlstad
  • 12,661
  • 10
  • 26
  • 39
Shammu
  • 81
  • 7
  • You didn't post the code that generates the string that gets displayed. When you format a number as a string you specify the number of digits in the format string. There are a lot of similar of not identical questions. Formatting numbers is explained in the docs as well. – Panagiotis Kanavos Nov 08 '22 at 12:03
  • `show the whole number` where do you show that number? Most UI controls/elements, whether desktop or web, support data binding *and* formatting – Panagiotis Kanavos Nov 08 '22 at 12:05
  • @PanagiotisKanavos: What has this to do with 'csvhelper'? – Poul Bak Nov 08 '22 at 12:09
  • 1
    Since you used the `CsvHelper` tag, are you reading this value from a CSV file? Or do you want to export the value to a CSV? Decimal digits don't matter in that case. If you want to open that file with Excel, the numbers will be displayed in whatever cell style Excel decides to use. A CSV is just a text file and has no formatting. In Excel all numbers are decimals and how they're displayed depends on the cell style – Panagiotis Kanavos Nov 08 '22 at 12:10
  • @PoulBak if the OP added that tag, it *does* have something to do with the question. `Display` may be a CSV or Excel file. We need to understand what the real problem is, not remove information we think isn't related to what we don't yet know – Panagiotis Kanavos Nov 08 '22 at 12:10
  • If you want to use Excel, use a library like EPPlus or ClosedXML to generate a real Excel file that allows you to style numbers any way you want. With both you can load data into a sheet with eg `var table=sheet.Cells.LoadFromCollection(orders)` and style specific columns. Check [this EPPlus example](https://github.com/EPPlusSoftware/EPPlus/wiki/LoadFromCollection) – Panagiotis Kanavos Nov 08 '22 at 12:11
  • @PanagiotisKanavos: Since OP says the answer below worked for him/her, you can remove that tag again. – Poul Bak Nov 08 '22 at 12:14
  • The [Formatting and Styling](https://github.com/EPPlusSoftware/EPPlus/wiki/Formatting-and-styling) example shows how to style numbers and use the built-in numeric formats. For example, `worksheet.Cells["C:C"].Style.Numberformat.Format = "0.00";` will set the built-in 2-digit format for the entire C column – Panagiotis Kanavos Nov 08 '22 at 12:16
  • If you want to print to the console using 2 digits, use `mean.ToString("0.00")` or better yet, `Console.WriteLine("The mean is {0:0.00}",mean);`. The number will be rounded to two digits. Try with `123.126`. You'll see that `123.13` will be printed – Panagiotis Kanavos Nov 08 '22 at 12:27

1 Answers1

1

try this:

  decimal a = 255;
  var result = Math.Round(a, 2).ToString("0.00");
Hossein Sabziani
  • 1
  • 2
  • 15
  • 20
  • 1
    Rounding is meaningless and will simply return the original number – Panagiotis Kanavos Nov 08 '22 at 12:04
  • @PanagiotisKanavos For 255 you are right. But if it has a decimal number, it must be rounded – Hossein Sabziani Nov 08 '22 at 12:06
  • 4
    It will be rounded by the format string. – Panagiotis Kanavos Nov 08 '22 at 12:06
  • 1
    @PanagiotisKanavos From the performance point of view, string format does not perform well. You round a number with a high number of decimals in a loop. Math.Round is faster – Hossein Sabziani Nov 08 '22 at 12:15
  • 1
    @hosseinsabziani this assertion is self-contradictory. You're using `String.Format` yourself. `String.Format` doesn't care what the value is, it will treat all decimals the same. For this particular example you're *wasting* time by rounding – Panagiotis Kanavos Nov 08 '22 at 12:17
  • @GeneralGrievance For the case where there is no decimal digit – Hossein Sabziani Nov 08 '22 at 13:45
  • what about `ToString("#.##")` ? – Vivek Nuna Nov 08 '22 at 13:54
  • @viveknuna `Math.Round` is faster than `ToString`. You can test this yourself. But in this example, since we must have two decimal digits for the integer number, I used `ToString`. There is probably a better way. – Hossein Sabziani Nov 08 '22 at 14:01
  • 1
    OK. I think I can clear this up. Internally, a decimal's `ToString("0.00")` method seems to call its own rounding method if necessary. Rounding first eliminates that need. My testing indicates that this is indeed faster. On older versions of .NET, there is a more significant speedup (~x1.41). On my current Core version, the difference is much less significant (~x1.2). – General Grievance Nov 08 '22 at 14:32
  • @GeneralGrievance Thank you for your follow up and explanation. I will again test String.Format with Math.Round in different modes. – Hossein Sabziani Nov 08 '22 at 14:39
  • 1
    No problem. Since this seems to be something that's not immediately obvious, it might be good to add this explanation to your answer. – General Grievance Nov 08 '22 at 14:54