Pregunta

I have done this for my division code, and instead of the numbers turning out like 10.55555 or 2.55555, how can it be shortened to 10.555 and 2.555

decimal d = numericUpDown1.Value / numericUpDown2.Value;
label1.Text = "Ratio: " + d.ToString();
¿Fue útil?

Solución

Try

decimal d = numericUpDown1.Value / numericUpDown2.Value ;
label1.Text = string.Format( "Ratio: {0}" ,
                Math.Round(d,3) ,
                MidpointRounding.ToEven
                ) ;

Or

decimal d = numericUpDown1.Value / numericUpDown2.Value ;
label1.Text = string.Format( "Ratio: {0:0.000}" ,
                Math.Round(d,3) ,
                MidpointRounding.ToEven
                ) ;
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top