C # Decimal Precision
C # for double decimals if you want to keep a certain precision, that is, the number of decimal places, you can convert to a string, the parameters are limited; The following program demonstrates this practice
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespace DoublePrecision//Calculates an average of an integer array, preserving two decimal places; classprogram {Static voidMain (string[] args) {int[] Nums = {1,2,3,4,5,6,9};DoubleAvg = nums. Average (); Console.WriteLine (avg);; Console.WriteLine ("{0:0.00}", avg);stringstr = avg. ToString ("0.00"); Console.WriteLine (str);; Console.readkey (); } }}
In fact, either with ToString or the normalized output is first converted to a string. So Console.WriteLine () will pass in a parameter that is followed by a colon after calling ToString.
Equivalent to Console.WriteLine (avg. ToString (0.00));
Run results
C # double-precision control