What is the easiest way to round off three numbers in a row without using iteration in C#?

Now it looks like this:
double a, b, c;
Console.WriteLine(Math.Round(a, 2)+" "+ Math.Round(b, 2)+" "+Math.Round(c, 2));

Is it possible to use Math.Round or format {0:F2} to array without loop? Or maybe there is another solution?
June 8th 19 at 17:08
1 answer
June 8th 19 at 17:10
No cycle just as you said (with the options of creating temporary variables with the results). Otherwise the desired cycle. But the cycle can be inside some function, for example, in the string.Join. And then, you still have to manually put data into the array.
double a = 10.123, b = 20.234, c = 30.345;
Console.WriteLine(Math.Round(a, 2) + "" + Math.Round(b, 2) + "" + Math.Round(c, 2));
Console.WriteLine(string.Join(" ", new []{a, b, c}.Select(x => x.ToString("F2"))));


You can also try to izgalnutsya - to put the data in the field and get the list of fields via reflection.
Thanks, didn't know about the reflection. - Arianna.Spor commented on June 8th 19 at 17:13
Only we must remember that the ToString("F2") is not rounding, and the number of digits in the fractional part, so rather Select(x => Math.Round(x, 2).ToString()) - Abner32 commented on June 8th 19 at 17:16

Find more questions by tags C#